mysql中表单查询用到select命令了,如果是多表查询我们用很多方法,如select union与left join之类的联合查询了,下面我给各位mysql初学者来介绍sql查询示例。
一、简单查询.
基本语法:SELECT * FROM tb_name;
查询全部:SELECT field1,field2 FROM tb_name;
投影代码如下:
SELECT [DISTINCT] * FROM tb_name WHERE qualification;
选择说明:FROM子句:要查询的关系 表、多个表、其它SELECT语句
WHERE子句:布尔关系表达式,主要包含如下这几类表达式:
- 比较:=、>、>=、<=、<
- 逻辑关系:
- AND
- OR
- NOT
- BETWEEN...AND...:在两个值之间
- LIKE‘’
- %:任意长度任意字符
- _:任意单个字符
- REGEXP,RLIKE:正则表达式,此时索引无效
- IN
- ISNULL
- ISNOTNULL
如下查询本博客的wp-links和wp_posts表,代码如下:
- mysql>select*fromwp_links;查询全部mysql>selectlink_name,link_urlfromwp_links;投影
- +-------------------+--------------------------------------+
- |link_name|link_url|
- +-------------------+--------------------------------------+
- |旺旺腾讯微博|http://www.phpfensi.com |
- |旺旺新浪微博|http://weibo.com/gz100ww|
- |51CTO技术博客|http://www.phpfensi.com/|
- +-------------------+--------------------------------------+
- 10rowsinset(0.00sec)
- mysql>selectID,post_title,post_datefromwp_postswhereID>1290andpost_status='publish';选择
- +------+----------------------------------------------------------------+----------------------+
- |ID|post_title|post_date|
- +------+----------------------------------------------------------------+----------------------+
- |1291|【转】HP3PAR存储概念之三|2013-08-2917:21:27|
- |1298|【转】HP3PAR存储概念之四|2013-08-2917:22:33|
- |1351|【转】XenDesktop5.5+vSphere5创建虚拟机报错|2013-09-0417:41:26|
- |1357|linux下强大的网络工具Netcat|2013-09-0922:26:45|
- |1360|MySQL常用命令、技巧和注意事项|2013-09-2011:04:15|
- |1369|【转】数据库设计原理知识--B树、B-树、B+树、B*树都是什么|2013-09-2112:30:18|
- |1379|MySQL基本SQL语句之常用管理SQL|2013-09-2112:39:23|
- +------+----------------------------------------------------------------+---------------------+
- 7rowsinset(0.01sec)
对查询结果排序:ORDER BY field_name {ASC|DESC},代码如下:
- mysql>selectID,post_title,post_datefromwp_postswhereID>1290andpost_status='publish'ORDERbyID;
- ##升序,ID是排序的字段
- mysql>selectID,post_title,post_datefromwp_postswhereID>1290andpost_status='publish'ORDERbyIDDESC;
- ##降序
- 字段别名:AS
- selectcol_nameASCOL_Aliases…:对字段使用别名
select col_name,… from tb_name AS tb_Aliases …:对表使用别名,如下:
- mysql>selectpost_titleAS文章标题fromwp_postswhereID>1290andpost_status='publish';
- +----------------------------------------------------------------+
- |文章标题|
- +----------------------------------------------------------------+
- |【转】HP3PAR存储概念之三|
- |【转】HP3PAR存储概念之四|
- |【转】XenDesktop5.5+vSphere5创建虚拟机报错|
- |linux下强大的网络工具Netcat|
- |MySQL常用命令、技巧和注意事项|
- |【转】数据库设计原理知识--B树、B-树、B+树、B*树都是什么|
- |MySQL基本SQL语句之常用管理SQL|
- +----------------------------------------------------------------+
- 7rowsinset(0.02sec)
- ##还可以这样:
- 代码如下复制代码
- mysql>select3+2ASSUM;
- +-----+
- |SUM|
- +-----+
- |5|
- +-----+
- 1rowinset(0.00sec)
LIMIT子句:LIMIT [offset,]Count,代码如下:
- mysql>selectID,post_title,post_datefromwp_postswhereID>1290andpost_status='publish'limit5;
- +------+--------------------------------------------------+---------------------+
- |ID|post_title|post_date|
- +------+--------------------------------------------------+---------------------+
- |1291|【转】HP3PAR存储概念之三|2013-08-2917:21:27|
- |1298|【转】HP3PAR存储概念之四|2013-08-2917:22:33|
- |1351|【转】XenDesktop5.5+vSphere5创建虚拟机报错|2013-09-0417:41:26|
- |1357|linux下强大的网络工具Netcat|2013-09-0922:26:45|
- |1360|MySQL常用命令、技巧和注意事项|2013-09-2011:04:15|
- +------+--------------------------------------------------+---------------------+
- 5rowsinset(0.01sec)
- mysql>selectID,post_title,post_datefromwp_postswhereID>1290andpost_status='publish'limit2,3;##红色部分(逗号前的数字)表示偏移量
- +------+--------------------------------------------------+---------------------+
- |ID|post_title|post_date|
- +------+--------------------------------------------------+---------------------+
- |1351|【转】XenDesktop5.5+vSphere5创建虚拟机报错|2013-09-0417:41:26|
- |1357|linux下强大的网络工具Netcat|2013-09-0922:26:45|
- |1360|MySQL常用命令、技巧和注意事项|2013-09-2011:04:15|
- +------+--------------------------------------------------+---------------------+
- 3rowsinset(0.00sec)
聚合:SUM(), MIN(), MAX(), AVG(), COUNT(),括号中为字段名
mysql> select sum(ID) from wp_posts;计算和
mysql> select min(ID) from wp_posts;查早最小的
mysql> select max(ID) from wp_posts; 查找最大的
mysql> select avg(ID) from wp_posts; 平均值
mysql> select count(ID) from wp_posts;计数
分组:GROUP BY,一般配合聚合运算使用,如下:
- mysql>selectcount(post_status)AS各状态数量,post_statusAS状态名称fromwp_postsgroupbypost_status;
- +-----------------+--------------+
- |各状态数量|状态名称|
- +-----------------+--------------+
- |1|auto-draft|
- |9|draft|
- |251|inherit|
- |238|publish|
- |2|trash|
- +-----------------+--------------+
- 5rowsinset(0.01sec)
注意:可以使用HAVING qualification将GROUP BY的结果再次过滤,用法同where.
二、多表查询
连接:
交叉连接:笛卡尔乘积
自然连接:将两张表某字段中相等连接起来,代码如下:
- mysql>SELECTstudents.Name,students.Age,courses.Cname,students.GenderFROMstudents,coursesWHEREstudents.CID1=courses.CID;
- +--------------+------+------------------+--------+
- |Name|Age|Cname|Gender|
- +--------------+------+------------------+--------+
- |GuoJing|19|TaiJiquan|M|
- |YangGuo|17|TaiJiquan|M|
- |DingDian|25|Qishangquan|M|
- |HuFei|31|Wanliduxing|M|
- |HuangRong|16|Qianzhuwandushou|F|
- |YueLingshang|18|Wanliduxing|F|
- |ZhangWuji|20|Hamagong|M|
- |Xuzhu|26|TaiJiquan|M|
- +--------------+------+------------------+--------+
- 8rowsinset(0.00sec)
外连接:
左外连接:left_tb LEFT JOIN right_tb ON ...:以左表为标准,代码如下:
- mysql>SELECTs.Name,c.CnameFROMstudentsASsLEFTJOINcoursesAScONs.CID1=c.CID;
- +--------------+--------------------+
- |Name|Cname|
- +--------------+--------------------+
- |GuoJing|TaiJiquan|
- |YangGuo|TaiJiquan|
- |DingDian|Qishangquan|
- |HuFei|Wanliduxing|
- |HuangRong|Qianzhuwandushou|
- |YueLingshang|Wanliduxing|
- |ZhangWuji|Hamagong|
- |Xuzhu|TaiJiquan|
- |LingHuchong|NULL|
- |YiLin|NULL|
- +--------------+--------------------+
- 10rowsinset(0.00sec)
右外连接:left_tb RIGHT JOIN right_tb ON ...:以右表为标准,代码如下:
- mysql>SELECTs.Name,c.CnameFROMstudentsASsRIGHTJOINcoursesAScONs.CID1=c.CID;
- +--------------+--------------------+
- |Name|Cname|
- +--------------+--------------------+
- |GuoJing|TaiJiquan|
- |YangGuo|TaiJiquan|
- |DingDian|Qishangquan|
- |HuFei|Wanliduxing|
- |HuangRong|Qianzhuwandushou|
- |YueLingshang|Wanliduxing|
- |ZhangWuji|Hamagong|
- |Xuzhu|TaiJiquan|
- |NULL|Yiyangzhi|
- |NULL|Jinshejianfa|
- |NULL|Qiankundanuoyi|
- |NULL|Pixiejianfa|
- |NULL|Jiuyinbaiguzhua|
- +--------------+--------------------+
- 13rowsinset(0.01sec)
自连接:本表中不同字段间进行连接,代码如下:
- mysql>SELECTc.NameASstudent,s.NameASteacherFROMstudentsASc,studentsASsWHEREc.TID=s.SID;
- +-----------+-------------+
- |student|teacher|
- +-----------+-------------+
- |GuoJing|DingDian|
- |YangGuo|GuoJing|
- |DingDian|ZhangWuji|
- |HuFei|HuangRong|
- |HuangRong|LingHuchong|
- +-----------+-------------+
- 5rowsinset(0.02sec)
注意:使用了别名
三、子查询:一个查询中嵌套另外一个查询
如下,在students表中查询年龄大于平均年龄的学生,代码如下:
- mysql>SELECTName,AgeFROMstudentsWHEREAge>(SELECTAVG(Age)FROMstudents);
- +-------------+------+
- |Name|Age|
- +-------------+------+
- |DingDian|25|
- |HuFei|31|
- |Xuzhu|26|
- |LingHuchong|22|
- +-------------+------+
- 4rowsinset(0.08sec)
子查询注意事项:
■比较操作中使用子查询:子查询只能返回单个值;
■IN():使用子查询;
■在FROM中使用子查询;
联合查询:UNION,将两个查询的结果合并,代码如下:
- mysql>(SELECTName,AgeFROMstudents)UNION(SELECTTname,AgeFROMtutors);
- +--------------+------+
- |Name|Age|
- +--------------+------+
- |GuoJing|19|
- |YangGuo|17|
- |DingDian|25|
- |HuFei|31|
- |HuangRong|16|
- |YueLingshang|18|
- |ZhangWuji|20|
- |HuYidao|42|
- |NingZhongze|49|
- +--------------+------+
- 19rowsinset(0.00sec)
- //开源代码phpfensi.com