SQL学习笔记二

来源:互联网 发布:做淘宝一件代发赚钱吗 编辑:程序博客网 时间:2024/06/09 19:57
1.between 关键字:
用于查询语句where条件中,需要获得两个值之间的数据:
那么可以使用:select 查询的字段名 from where 条件字段 between 值1 and 值2 ;
例如:查询student表中id字段901-905之间的数据。
mysql> select id from student where id between '901' and '905';
+------+
| id   |
+------+
|  901 |
|  902 |
|  903 |
|  904 |
|  905 |
+------+
2.Alias (别名)关键字:
用于个指定的表或者字段重新取一个新的名字,只要方便阅读。
使用格式:select 字段名 AS 字段别名,...from 表名;
例如:查询student 表中id,name字段的全部内容,并分别取stu_id,stu_name做为别名
mysql> select id as stu_id,name as stu_name from student ;
+--------+----------+
| stu_id | stu_name |
+--------+----------+
|    901 | 张老大   |
|    902 | 李二     |
|    903 | 张三     |
|    904 | 李四     |
|    905 | 王武     |
|    906 | 王六     |
|    907 | 小雪     |
+--------+----------+
3.Join 关键字:
通常一般数据都会放到几张不同的表中,而表与表之间又有一定的联系,这时我们将会同时访问多张表
并将结果返回到一个结果集合中来,此时我们就可以使用多表查询加上where条件限定来完成。(当然这样的方法条件


一多就显得很冗长,这时我们就可以使用Join关键字)
使用格式:select 字段名,... from 表名1,表名2 where 表1.字段=表2.字段;
例如:查询student表中id字段与score 表中 cource,grade字段内容;
mysql> select student.id,student.name,score.cource,score.grade
from student,score where student.id=score.id;
+------+--------+--------+-------+
| id   | name   | cource | grade |
+------+--------+--------+-------+
|  901 | 张老大 | 计算机 |    98 |
|  901 | 张老大 | 英语   |    80 |
|  902 | 李二   | 计算机 |    65 |
|  902 | 李二   | 中文   |    88 |
|  903 | 张三   | 中文   |    95 |
|  904 | 李四   | 计算机 |    70 |
|  904 | 李四   | 英语   |    92 |
|  905 | 王武   | 英语   |    94 |
|  906 | 王六   | 计算机 |    90 |
|  906 | 王六   | 英语   |    85 |
+------+--------+--------+-------+
3.1 Inner Join 关键字(也可以只用Join表示)
是用于左右表都匹配的情况下就返回数据,反之就不显示。
使用格式:select * from 表名1 (Inner) join 表名2 on 表1.字段名=表2.字段名;
例如:查询student表中id,name字段,表score中cource,grade字段内容;
mysql> select st.id,st.name,c.cource,c.grade from student as st join score as con st.id=c.id;
+------+--------+--------+-------+
| id   | name   | cource | grade |
+------+--------+--------+-------+
|  901 | 张老大 | 计算机 |    98 |
|  901 | 张老大 | 英语   |    80 |
|  902 | 李二   | 计算机 |    65 |
|  902 | 李二   | 中文   |    88 |
|  903 | 张三   | 中文   |    95 |
|  904 | 李四   | 计算机 |    70 |
|  904 | 李四   | 英语   |    92 |
|  905 | 王武   | 英语   |    94 |
|  906 | 王六   | 计算机 |    90 |
|  906 | 王六   | 英语   |    85 |
+------+--------+--------+-------+
3.2 Left join 关键字
用于左表所有数据都要返回到结果集中,如果右表中不匹配则用NULL填充。
使用格式:select * from 表名1(左表) left join 表名2(右表) on 左表.字段名=右表。字段名;
例如:查询student(左表) 中id,name字段,score(右表)中的grade字段内容;
mysql> select student.id, student.name,score.grade from student left join score
on student.id=score.id;(可以看到根据左表ID来返回结果集,score右表中没有907这个id内容所


以就返回NULL)
+------+--------+-------+
| id   | name   | grade |
+------+--------+-------+
|  901 | 张老大 |    98 |
|  901 | 张老大 |    80 |
|  902 | 李二   |    65 |
|  902 | 李二   |    88 |
|  903 | 张三   |    95 |
|  904 | 李四   |    70 |
|  904 | 李四   |    92 |
|  905 | 王武   |    94 |
|  906 | 王六   |    90 |
|  906 | 王六   |    85 |
|  907 | 小雪   |  NULL |
+------+--------+-------+
3.3 Right Join 关键字
与Left join 恰恰相反,右表所有数据都要返回左表没有的数据用NULL表示
使用格式:select * from 表名1(左表) right join 表名2(右表) on 左表.字段名=右表。字段名;
例如:查询student(左表) 中id,name字段,score(右表)中的grade字段内容;
mysql> select student.id,student.name,score.grade from student right join score
on student.id=score.id;(可以看到根据右表ID来返回结果集,student左表中没有908这个id内容所以就返回


NULL)
+------+--------+-------+
| id   | name   | grade |
+------+--------+-------+
|  901 | 张老大 |    98 |
|  901 | 张老大 |    80 |
|  902 | 李二   |    65 |
|  902 | 李二   |    88 |
|  903 | 张三   |    95 |
|  904 | 李四   |    70 |
|  904 | 李四   |    92 |
|  905 | 王武   |    94 |
|  906 | 王六   |    90 |
|  906 | 王六   |    85 |
| NULL | NULL   |    99 |
+------+--------+-------+
3.4 Full Join 关键字
用于只要表中有一个数据存在匹配就返回结果集中(注:MYSQL5.1以上版本才可以使用)
使用格式:select * from 表名1 full join 表名2 on 表1.字段名=表2.字段名;
例如:查询student(左表) 中id,name字段,score(右表)中的grade字段内容;
mysql> select student.id,student.name,score.grade from student full join score

on student.id=score.id;(这时表中所有存在的数据将会被返回)

4.Union (联合)关键字
有时我们查询某条数据时可能发现一条语句搞不定,那么Union可以来帮忙
Union一般用于连接两个及其以上的查询语句
使用格式:(SELECT 字段名 FROM 表名)UNION [ALL] (SELECT 字段名 FROM 表名);
例如:查询student 表中id 字段901-903升序字段,并且查询904-906之间降序排列的字段。
mysql> (select id from student where id between '901' and '903' order by id limit 10)
union
(select id from student where id between '904' and '906' order by id desc limit 10);
+------+
| id   |
+------+
|  901 |
|  902 |
|  903 |
|  906 |
|  905 |
|  904 |
+------+ 
注: 1.两次查询的字段数量必须一致,要不然没法确定返回的结果集字段。
      2.union 如果不加ALL的话会自动帮你省略掉查询到的重复数据,如果想让全部显示就加上ALL。
      3.在联合语句中使用order by进行排序那么就必须跟limit一起使用否则排序无效。
      4.返回的结果集是用第一个表的查询字段来名返回,这也是要求查询的字段数量一致的原因。

0 0
原创粉丝点击