您的位置:首页 > 数据库 > MySQL

MySQL常见查询语句

2015-07-30 11:21 716 查看
数据库在开发中基本上都会用到,常用的MySQL、Oracle、SQL Server等等,这里主要了解MySQL,基础的东西就不说了,主要做个基本的总结。像select/where/group by/having/order by这些就自己去看了。

where针对表中的列发挥作用,查询数据

having对查询结果中的列发挥作用,筛选数据

1.mysql五种聚集函数,max min count avg sum

常见问题:

1.查询时强制使用区分大小写(/article/6596878.html)

select * from people where binary name like ‘%a’;

select * from people where binary name like ‘%A’;

2.查询数据表中的某个字段的重复记录:

mysql> select * from people;

+—-+——+——+——–+——+———————+

| id | name | age | addr | sex | birthday |

+—-+——+——+——–+——+———————+

| 1 | aa | 12 | 南昌 | 男 | 2015-07-17 18:30:30 |

| 2 | bb | 19 | 北京 | 女 | 2015-07-14 18:30:34 |

| 3 | cc | 17 | 武汉 | 男 | 2015-07-14 18:30:39 |

| 4 | dd | 21 | 长沙 | 男 | 2015-07-01 18:30:44 |

| 5 | ee | 32 | 合肥 | 女 | 2015-07-18 14:56:34 |

| 6 | cc | 17 | 武汉 | 男 | 2015-07-14 18:30:39 |

| 7 | ff | 43 | 广州 | 女 | 2015-07-23 11:10:46 |

| 8 | aa | 12 | 南昌 | 男 | 2015-07-17 18:30:30 |

| 9 | cc | 17 | 武汉 | 男 | 2015-07-14 18:30:39 |

+—-+——+——+——–+——+———————+

mysql> select name,count(name) from people group by name having count(name)>1;

+——+————-+

| name | count(name) |

+——+————-+

| aa | 2 |

| cc | 3 |

+——+————-+

3.查询(删除)除了id不同其他字段都相同的数据

mysql> select min(id) from (select * from people) as t group by t.name,t.age,t.addr,t.sex,t.birthday;

+———+

| min(id) |

+———+

| 1 |

| 2 |

| 3 |

| 4 |

| 5 |

| 7 |

+———+

要把其中查询到的不重复的东西做成一个虚表。

mysql> delete from people where id not in (select min(id) from (select * from people) as t group by

t.name,t.age,t.addr,t.sex,t.birthday);

Query OK, 3 rows affected (0.09 sec)

mysql> select * from people;

+—-+——+——+——–+——+———————+

| id | name | age | addr | sex | birthday |

+—-+——+——+——–+——+———————+

| 1 | aa | 12 | 南昌 | 男 | 2015-07-17 18:30:30 |

| 2 | bb | 19 | 北京 | 女 | 2015-07-14 18:30:34 |

| 3 | cc | 17 | 武汉 | 男 | 2015-07-14 18:30:39 |

| 4 | dd | 21 | 长沙 | 男 | 2015-07-01 18:30:44 |

| 5 | ee | 32 | 合肥 | 女 | 2015-07-18 14:56:34 |

| 7 | ff | 43 | 广州 | 女 | 2015-07-23 11:10:46 |

+—-+——+——+——–+——+———————+

或者:多查询一遍

mysql> delete from people where id not in (select minid from (select min(id) as minid from people gr

oup by name,age,addr,sex,birthday) b );

Query OK, 0 rows affected (0.18 sec)

4.查找学生姓名/平均成绩,其有两门或两门以上的课程不及格

http://bbs.csdn.net/topics/330128774

mysql> select * from stu;

+—–+——–+

| sid | sn |

+—–+——–+

| 1 | 张三 |

| 2 | 李四 |

| 3 | 王五 |

| 4 | 赵六 |

+—–+——–+

4 rows in set (0.00 sec)

mysql> select * from course;

+—–+——–+

| cid | cn |

+—–+——–+

| 1 | 语文 |

| 2 | 数学 |

| 3 | 英语 |

| 4 | 物理 |

| 5 | 化学 |

| 6 | 生物 |

+—–+——–+

6 rows in set (0.00 sec)

mysql> select * from sc;

+—–+—–+——-+

| sid | cid | score |

+—–+—–+——-+

| 1 | 1 | 80 |

| 1 | 2 | 87 |

| 1 | 3 | 92 |

| 1 | 4 | 77 |

| 2 | 1 | 45 |

| 2 | 2 | 55 |

| 2 | 3 | 59 |

| 2 | 4 | 47 |

| 3 | 1 | 52 |

| 3 | 2 | 96 |

| 3 | 3 | 49 |

| 3 | 4 | 77 |

| 4 | 1 | 61 |

| 4 | 2 | 72 |

| 4 | 3 | 83 |

| 4 | 4 | 88 |

+—–+—–+——-+

16 rows in set (0.00 sec)

查询方式:

mysql> select stu.sid,stu.sn,avg(sc.score) from stu,sc where stu.sid=sc.sid group by stu.sid,stu.sn

having count(case when sc.score<60 then 1 end)>=2;

+—–+——–+—————+

| sid | sn | avg(sc.score) |

+—–+——–+—————+

| 2 | 李四 | 51.5000 |

| 3 | 王五 | 68.5000 |

+—–+——–+—————+

2 rows in set (0.14 sec)

mysql> select sid,sn from stu where sid in (select sid from sc where score<60 group by sid having count(cid)>=2);

+—–+——–+

| sid | sn |

+—–+——–+

| 2 | 李四 |

| 3 | 王五 |

+—–+——–+

mysql> select stu.sn,avg(sc.score) from stu,sc where stu.sid in (select sc.sid from sc where sc.score<60 group by sc.sid having count(sc.cid)>=2) and stu.sid=sc.sid group by stu.sn;

+——–+—————+

| sn | avg(sc.score) |

+——–+—————+

| 李四 | 51.5000 |

| 王五 | 68.5000 |

+——–+—————+

2 rows in set (0.00 sec)

5.排序,分页,递归查询(period)

排序:order by

分页:limit x,y x从0开始计算 y为要取出的个数

递归查询:

如果知道了有多少层,可以用union来操作;

利用函数/函数(存储过程)http://blog.csdn.net/jackiehome/article/details/6803978

6.连接查询的方式,各种方式的区别,union(all)

(1)内连接:

等值连接:在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询结果中列出被连接 表中的所有列,包括其中的重复列。

不等连接:在连接条件使用除等于运算符以外的其它比较运算符比较被连接的列的列值。这些 运算符包括>、>=、<=、<、!>、!<和<>。

自然连接:在连接条件中使用等于(=)运算符比较被连接列的列值,但它使用选择列表指出查询 结果集合中所包括的列,并删除连接表中的重复列。

(2)外连接:左外连接(left [outer] join),右外连接(right [outer] join)

(3)交叉连接(cross join),不能使用where on来操作,返回笛卡尔积

union:会去除重复项

union all:不会去除重复项,所有的都会展示出来

some any all

参考—/article/2313065.html

7.全文本搜索

MyISAM(支持全文本搜索) InnoDB(支持事务)

8.查询每个月的销售额都大于id为1001的产品的产品id和产品名称(共12个月)(1月份的销售额大于1001的1月份的销售额,2月份的大于2月份的,依此类推……)

id—序号id proid—产品id month—月份 sale—销售额

mysql> select * from sales;

+—-+——-+——-+——+

| id | proid | month | sale |

+—-+——-+——-+——+

| 1 | 1001 | 1 | 1000 |

| 2 | 1001 | 2 | 1100 |

| 3 | 1001 | 3 | 1200 |

| 4 | 1001 | 4 | 1300 |

| 5 | 1001 | 5 | 1400 |

| 6 | 1001 | 6 | 1500 |

| 7 | 1001 | 7 | 1600 |

| 8 | 1001 | 8 | 1700 |

| 9 | 1001 | 9 | 1800 |

| 10 | 1001 | 10 | 1900 |

| 11 | 1001 | 11 | 2000 |

| 12 | 1001 | 12 | 2100 |

| 13 | 1002 | 1 | 2000 |

| 14 | 1002 | 2 | 2100 |

| 15 | 1002 | 3 | 2000 |

| 16 | 1002 | 4 | 2100 |

| 17 | 1002 | 5 | 2000 |

| 18 | 1002 | 6 | 2000 |

| 19 | 1002 | 7 | 2000 |

| 20 | 1002 | 8 | 2000 |

| 21 | 1002 | 9 | 2000 |

| 22 | 1002 | 10 | 2000 |

| 23 | 1002 | 11 | 2300 |

| 24 | 1002 | 12 | 3000 |

| 25 | 1003 | 1 | 1700 |

| 26 | 1003 | 2 | 2000 |

| 27 | 1003 | 3 | 2000 |

| 28 | 1003 | 4 | 1800 |

| 29 | 1003 | 5 | 2000 |

| 30 | 1003 | 6 | 1500 |

| 31 | 1003 | 7 | 2000 |

| 32 | 1003 | 8 | 2000 |

| 33 | 1003 | 9 | 1800 |

| 34 | 1003 | 10 | 2000 |

| 35 | 1003 | 11 | 2300 |

| 36 | 1003 | 12 | 2200 |

+—-+——-+——-+——+

步骤1:查找对应月份大于1001的销售额的信息;

mysql> select * from sales s where s.sale > (select p.sale from sales p where p.proid = 1001 and p

.month = s.month);

+—-+——-+——-+——+

| id | proid | month | sale |

+—-+——-+——-+——+

| 13 | 1002 | 1 | 2000 |

| 14 | 1002 | 2 | 2100 |

| 15 | 1002 | 3 | 2000 |

| 16 | 1002 | 4 | 2100 |

| 17 | 1002 | 5 | 2000 |

| 18 | 1002 | 6 | 2000 |

| 19 | 1002 | 7 | 2000 |

| 20 | 1002 | 8 | 2000 |

| 21 | 1002 | 9 | 2000 |

| 22 | 1002 | 10 | 2000 |

| 23 | 1002 | 11 | 2300 |

| 24 | 1002 | 12 | 3000 |

| 25 | 1003 | 1 | 1700 |

| 26 | 1003 | 2 | 2000 |

| 27 | 1003 | 3 | 2000 |

| 28 | 1003 | 4 | 1800 |

| 29 | 1003 | 5 | 2000 |

| 31 | 1003 | 7 | 2000 |

| 32 | 1003 | 8 | 2000 |

| 34 | 1003 | 10 | 2000 |

| 35 | 1003 | 11 | 2300 |

| 36 | 1003 | 12 | 2200 |

+—-+——-+——-+——+

步骤2:在上述的基础上以proid为组,使用count计数;

mysql> select proid,count() from (select from sales s where s.sale > (select p.sale from sales p

where p.proid = 1001 and p.month = s.month)) ss group by proid;

+——-+———-+

| proid | count(*) |

+——-+———-+

| 1002 | 12 |

| 1003 | 10 |

+——-+———-+

步骤3:完成上一步之后,只要满足count(*)=12的都是满足条件的;

mysql> select proid from (select * from sales s where s.sale > (select p.sale from sales p where p.

proid = 1001 and p.month = s.month)) ss group by proid having count(*)=12;

+——-+

| proid |

+——-+

| 1002 |

+——-+

参考:

/article/4657950.html

/article/8360828.html

/article/10602085.html

/article/8608673.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: