mysql查询
(1)查询数据表的所有记录
select * from students;
(2)查询特定字段的信息
select ke_name,ke_tea_num from cources;
+----------+------------+
| ke_name | ke_tea_num |
+----------+------------+
| ke_one | 3014 |
| ke_two | 3011 |
| ke_three | 3013 |
| ke_four | 3012 |
+----------+------------+
(3)查询并排重——distinct关键字
select distinct num from score;
(4)查询区间——where +between +and 或者 where > 80 and < (直接使用运算符比较)
select * from score where score between 80 and 90;
select * from score where score> 80 and score < 90;
(5)查询 或 关系,什么或什么或什么——in( , , )
select * from score where score in(85,86,87);
(6)不同字段的 或 关系——or 关键字
select * from score where ke_num=2 or score >80;
(7) 不同字段的且 关系——and关键字
mysql> select * from score where ke_num=2 and score >80;
(8)升序降序查询——order by asc为升序,order by desc为降序
select * from score where ke_num=2 order by score desc;
设定两列一列升序、一列降序——固定格式:order by A asc,B desc;(逗号不能换成and)
select * from score order by ke_num asc,score desc;
+------+--------+-------+
| num | ke_num | score |
+------+--------+-------+
| 2001 | 1 | 93 |
| 2005 | 1 | 87 |
| 2007 | 1 | 58 |
| 2004 | 2 | 91 |
| 2009 | 2 | 84 |
| 2002 | 2 | 83 |
| 2005 | 2 | 71 |
| 2007 | 3 | 97 |
| 2001 | 3 | 84 |
| 2006 | 3 | 78 |
| 2003 | 3 | 74 |
| 2008 | 4 | 95 |
| 2003 | 4 | 94 |
| 2009 | 4 | 91 |
+------+--------+-------+
(9)统计查询
mysql> select count(*) from score where score between 80 and 90;
(10)max min查询——max()或min()或者排序查询
select max(score) from score ;
+------------+
| max(score) |
+------------+
| 97 |
+------------+
当最值出现重复时,
select * from students where stu_num=(select num from score where score=(select max(score) from score));
ERROR 1242 (21000): Subquery returns more than 1 row
使用非排序的方法可以分两步:
select num from score where score=(select max(score) from score);
select * from students where stu_num in(2004,2007);
合起来用一个in关键字就能实现
select * from students where stu_num in (select num from score where score=(select max(score) from score));
使用排序的方法:
select num from score order by score desc limit 0,5;
但是limit和in不能一起使用。只能分两步。
select * from students where stu_num in(select num from score order by score desc limit 0,5);
ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
(11)子查询
select num from score where score=(select max(score) from score);
+------+
| num |
+------+
| 2007 |
+------+
select * from students where stu_num=(select num from score where score=(select max(score) from score));
+---------+----------+
| stu_num | stu_name |
+---------+----------+
| 2007 | xiaoqi |
+---------+----------+
(12)查询平均、分组平均
select avg(score)from score where ke_num=2;
+------------+
| avg(score) |
+------------+
| 83.7500 |
+------------+
分组平均——group by关键字
select avg(score) from score group by ke_num;
select ke_num, avg(score) from score group by ke_num;
+--------+------------+
| ke_num | avg(score) |
+--------+------------+
| 1 | 79.3333 |
| 2 | 83.7500 |
| 3 | 83.2500 |
| 4 | 93.3333 |
+--------+------------+
至少有几条记录——having count(*)关键字
select ke_num,avg(score)from score group by ke_num having count(*)>=4;//至少有几条记录
(13)like关键字用于匹配
select tea_name from teachers where tea_name like 'shi%';
+----------+
| tea_name |
+----------+
| shiyi |
| shier |
| shisan |
| shisi |
| shiwu |
+----------+
(14)查询不同的表中的字段对某一字段的限制——多表查询
//从students中选出,score表中成绩大于90的同学。
select stu_name,ke_num,score from students,score
-> where students.stu_num=score.num and score.score>90;
(15)查询所有学生的姓名、课程名称、成绩、任课教师姓名
select stu_name,ke_name,score,tea_name from students,cources,score,teachers
-> where score.num=students.stu_num and score.ke_num=cources.ke_num and cources.ke_tea_num=teachers.tea_num;
查表先分开查询,然后按照两表的共有字段进行匹配,最终把表合并。
(16)查询选修1课程并且比2007号同学1课程成绩高的同学。
select * from score where ke_num=1 and score>(select score from score where num=2007 and ke_num=1);
+------+--------+-------+
| num | ke_num | score |
+------+--------+-------+
| 2001 | 1 | 93 |
| 2005 | 1 | 87 |
+------+--------+-------+
(17)查询与年份有关的
select year(birthday) from students where sno in(108,101);
(18)查询某任课老师所教授课程的名称
select ke_name from cources where ke_tea_num=(select tea_num from teachers where tea_name='shiyi');
(19)查询选修某门课的学生数大于3的教师的名单
select tea_name from teachers where tea_num in(select ke_tea_num from cources where ke_num in(select ke_num from score group by ke_num having count(*)>3));
+----------+
| tea_name |
+----------+
| shiyi |
| shisan |
+----------+
(20)查看女老师教授的课程的得分情况
select * from score where ke_num in(select ke_num from cources where ke_tea_num in (select tea_num from teachers where tea_sex='F' ));