继上一篇:【学习笔记】——Ubuntu下的MySQL命令(一)
之前的文章用的抽象的语法格式,确实不太方便。
下面的命令以
表名为table
字段名:id name gender age height isdelete
的表为例。
1、内连接: 只显示满足 on 后面的条件的数据。
select * from student as s inner join classes as c on s.c_id = s.id
2、左连接: 以左表为主,若右表数据不存在填空
select * from students as s left jion classes as c on s.c_id = s.id;
3、右连接:以右表数据为主,若右表不存在填空
select * from students as s right join classes as c on s.c_id = s.id;
4、自连接:查询到而两张表是同一张表。必须要给表起别名。
select c.id, c.title, c.pid, p.title from areas as c inner join areas as p on c.pid = p.id;
5、子查询:
在一个selcet语句中,嵌入另一个select语句,被插入的select语句叫子查询,外部select叫主查询。
查询大于平均年龄的学生:
1、查询平均年龄
select avg(age) from student;
2、查询大于平均年龄的学生
select * from student where age>(select avg(age) from student);
外键约束:
作用:对外见字段的值进行更新和插入时会引用表中字段的数据进行验证,数据不合法更新和插入就会失败,
保证数据的有效性。
1、对已存在的字段添加外键约束
alter table 表名 add foreign key(外键字段) references 表二(id);
2、创建数据表时设置外键
create table 表名1( id int not null primary key [auto_increment], ...); create table 表名2( id int not null primary key [auto_increment], 字段二 ,... foreign key (外键字段) references 表名1(id));
3、删除外键约束
alter table 表名2 drop foreign key 外键名;
创建外键时,系统会自动生成外键名。获取外键名的方法:
show create table 表名;
分组+聚合
(下面我要偷懒了!!!)
mysql> select name,price from goods where cate_name='超级本';
+-----------------------------+----------+
| name | price |
+-----------------------------+----------+
| x240 超极本 | 4880.000 |
| u330p 13.3英寸超极本 | 4299.000 |
| svp13226scb 触控超极本 | 7999.000 |
+-----------------------------+----------+
3 rows in set (0.00 sec)
mysql> select cate_name from goods group by cate_name;
+---------------------+
| cate_name |
+---------------------+
| 台式机 |
| 平板电脑 |
| 服务器/工作站 |
| 游戏本 |
| 笔记本 |
| 笔记本配件 |
| 超级本 |
+---------------------+
7 rows in set (0.00 sec)
mysql> select round(avg(price),2) from goods;
+---------------------+
| round(avg(price),2) |
+---------------------+
| 5570.57 |
+---------------------+
1 row in set (0.00 sec)
mysql> select cate_name,avg(price) from goods group by cate_name;
+---------------------+---------------+
| cate_name | avg(price) |
+---------------------+---------------+
| 台式机 | 4821.2500000 |
| 平板电脑 | 2724.6666667 |
| 服务器/工作站 | 11363.0000000 |
| 游戏本 | 8499.0000000 |
| 笔记本 | 3732.3333333 |
| 笔记本配件 | 2399.0000000 |
| 超级本 | 5726.0000000 |
+---------------------+---------------+
7 rows in set (0.00 sec)
mysql> select cate_name,max(price),min(price),avg(price),count(price) from goods group by cate_name;
+---------------------+------------+------------+---------------+--------------+
| cate_name | max(price) | min(price) | avg(price) | count(price) |
+---------------------+------------+------------+---------------+--------------+
| 台式机 | 9188.000 | 2899.000 | 4821.2500000 | 4 |
| 平板电脑 | 3388.000 | 1998.000 | 2724.6666667 | 3 |
| 服务器/工作站 | 28888.000 | 4288.000 | 11363.0000000 | 4 |
| 游戏本 | 8499.000 | 8499.000 | 8499.0000000 | 1 |
| 笔记本 | 4999.000 | 2799.000 | 3732.3333333 | 3 |
| 笔记本配件 | 6999.000 | 99.000 | 2399.0000000 | 3 |
| 超级本 | 7999.000 | 4299.000 | 5726.0000000 | 3 |
+---------------------+------------+------------+---------------+--------------+
7 rows in set (0.02 sec)
mysql> select id,name,price from goods where price>(select avg(price) from goods) order by price desc;
+----+---------------------------------------+-----------+
| id | name | price |
+----+---------------------------------------+-----------+
| 17 | mac pro专业级台式电脑 | 28888.000 |
| 13 | imac me086ch/a 21.5英寸一体电脑 | 9188.000 |
| 3 | g150th 15.6英寸游戏本 | 8499.000 |
| 7 | svp13226scb 触控超极本 | 7999.000 |
| 18 | hmz-t3w 头戴显示设备 | 6999.000 |
| 20 | x3250 m4机架式服务器 | 6888.000 |
+----+---------------------------------------+-----------+
6 rows in set (0.00 sec)