PHP之路——MySql查询语句
Posted 偏执Code
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP之路——MySql查询语句相关的知识,希望对你有一定的参考价值。
1,select查询的基本结构
select 字段 from 表 where 过滤条件 group by 分组条件 order by 排序条件 having 过滤的第二条件 limit 限定结果条件;
2,最简单的查询语句
mysql> select * from user; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+
3,查询一列或多列
mysql> select username,createtime from user; +----------+---------------------+ | username | createtime | +----------+---------------------+ | kenan | 2012-10-31 15:32:26 | | kenan | 2012-10-31 15:32:26 | +----------+---------------------+ 2 rows in set (0.00 sec)
4,多表查询
mysql> select * from user; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec) mysql> select * from teacher; +----+------------+---------------------+ | id | username | createtime | +----+------------+---------------------+ | 1 | Teacher Li | 2012-11-01 21:00:58 | | 2 | Teacher Le | 2012-11-01 21:02:07 | +----+------------+---------------------+ 2 rows in set (0.00 sec) mysql> select * from user,teacher; +----+----------+----------+---------------------+----+------------+---------------------+ | id | username | password | createtime | id | username | createtime | +----+----------+----------+---------------------+----+------------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 | | 3 | kenan | lele | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 | | 1 | kenan | kenan | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 | | 3 | kenan | lele | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 | +----+----------+----------+---------------------+----+------------+---------------------+ 4 rows in set (0.00 sec) 多表查询中,如果没有限制条件的话,两个表的记录会分别进行匹配,产生的结果就是两个表的笛卡尔积,叫做全连接 下面的这个带有条件的叫做等同连接,相当于在上面的查询结果中挑选出来满足where后面条件的记录,因为没有所有是空的结果。
mysql> select * from user,teacher where user.username = teacher.username; Empty set (0.00 sec)
5,带有简单的where字句条件查询
mysql> select * from user where id>2; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec)
6,带有in的查询,in用来表示范围
mysql> select * from user where username in (‘kenan‘,‘Micheal‘); +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec)
这里查询出来username是kenan或者是Micheal的记录
7,between and 用来表示一个字段的范围的查询
mysql> select * from user where id between 2 and 3; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec) mysql> select * from user where id between 3 and 4; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec)
这里分别查询出来id在2和3之间的记录,3到4之间的记录,从结果可以看出 between and是包含边界的,就是>=2而且<=3
8, is null 查询空值
mysql> select * from user ; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | | 4 | lele | lele | NULL | +----+----------+----------+---------------------+ 3 rows in set (0.00 sec) mysql> select * from user where createtime is null; +----+----------+----------+------------+ | id | username | password | createtime | +----+----------+----------+------------+ | 4 | lele | lele | NULL | +----+----------+----------+------------+ 1 row in set (0.00 sec)
9,用and连接多条件查询 并关系
mysql> select * from user where username = ‘kenan‘ and password = ‘kenan‘; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec)
10,用or 的多条件查询 这个是或关系,满足一个条件即可
mysql> select * from user where username = ‘kenan‘ or password = ‘kenan‘; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec)
11,聚合函数 count()用来统计总数
mysql> select count(*) from user; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.00 sec)
12,使用sum()求和,使用avg函数求平均值
mysql> select id from user; +----+ | id | +----+ | 1 | | 3 | | 4 | +----+ 3 rows in set (0.00 sec) mysql> select sum(id) from user; +---------+ | sum(id) | +---------+ | 8 | +---------+ 1 row in set (0.00 sec) mysql> select avg(id) from user; +---------+ | avg(id) | +---------+ | 2.6667 | +---------+ 1 row in set (0.04 sec)
13,使用max()求最大值,使用min求最小值
mysql> select id from user; +----+ | id | +----+ | 1 | | 3 | | 4 | +----+ 3 rows in set (0.00 sec) mysql> select max(id) from user; +---------+ | max(id) | +---------+ | 4 | +---------+ 1 row in set (0.00 sec) mysql> select min(id) from user; +---------+ | min(id) | +---------+ | 1 | +---------+ 1 row in set (0.00 sec)
原文链接:http://soukenan.blog.51cto.com/5130995/1047201
以上是关于PHP之路——MySql查询语句的主要内容,如果未能解决你的问题,请参考以下文章