2018-07-10聚合函数+比较条件+''和NULL+DISTINCT+ORDER BY+LIMIT+GROUP BY
Posted postgredingdangniu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-07-10聚合函数+比较条件+''和NULL+DISTINCT+ORDER BY+LIMIT+GROUP BY相关的知识,希望对你有一定的参考价值。
‘‘表示空字符串!
NULL表示值未知!
%表示任意个字符
_表示一个字符
COUNT(*)包括NULL值(会把所有列都统计一遍,取最大值)!
COUNT(字段)不包括NULL值!
SELECT * FROM PRODUCT
//查询所有列,*表示所有列!
SELECT PRO_NAME,PROTYPE_ID FROM PRODUCT
//只查询其中两列!
SELECT PRO_NAME AS 产品名称,PRICE AS 价格 FROM PRODUCT
//用AS给字段添加别名!
SELECT (PRO_ID+PROTYPE_ID) AS 测试 FROM PRODUCT
//两列合并(相加),只适合数值类型的列!
强调:用数据库可以解决的问题,尽量不要用程序来解决(这样会不会增加服务器负担)!
SELECT DISTINCT SEX AS 性别 FROM STUDENT
//运用DISTINCT去重!
SELECT * FROM STUDENT WHERE SEX=‘男‘ AND SNAME=‘赵日天‘
//查询性别是男并且名字是赵日天的所有字段信息!
SELECT * FROM STUDENT WHERE SEX=‘女‘ OR SNAME=‘赵日天‘
//查询性别是女或者名字是赵日天的所有字段信息!
###比较运算符:< > = <= >= <> !=
SELECT * FROM STUDENT WHERE SERVLET<70
//查询SERVLET成绩小于70的所有字段信息!
SELECT * FROM STUDENT WHERE JSP<>60
//查询JSP成绩不等于60的所有字段信息!
SELECT * FROM STUDENT WHERE html BETWEEN 90 AND 100
SELECT * FROM STUDENT WHERE HTML>=90 AND HTML<=100
//查询HTML成绩介于90到100之间的所有字段信息!
SELECT * FROM STUDENT WHERE JSP IS NULL
SELECT * FROM STUDENT WHERE SEX IS NOT NULL
//查询JSP是NULL的和SEX不是NULL的结果集!
SELECT * FROM STUDENT WHERE SNAME=‘‘
//查询SNAME是空字符串的所有字段信息!
SELECT * FROM STUDENT WHERE SNAME=‘‘ OR SNAME IS NULL
//注意空字符串和NULL值,重点注意!
SELECT * FROM STUDENT WHERE SNAME LIKE ‘李%‘
SELECT * FROM STUDENT WHERE SNAME LIKE ‘李_‘
//查询姓名是李开头的所有字段信息;查询姓名李开头,但是名字只有两个字的所有字段的信息!
SELECT * FROM STUDENT WHERE SNAME LIKE ‘__‘
//查询表中名字只有两个字的同学的所有字段的信息!
/*聚合函数*/
SELECT SUM(SERVLET) AS SERVLET总成绩 FROM STUDENT
SELECT AVG(SERVLET) AS SERVLET平均成绩 FROM STUDENT
SELECT MAX(SERVLET) AS SERVLET最高成绩 FROM STUDENT
SELECT MIN(SERVLET) AS SERVLET最低成绩 FROM STUDENT
SELECT COUNT(SERVLET) AS SERVLET成绩个数 FROM STUDENT
SUM:求和
AVG:求平均数
MAX:求最大值
MIN:求最小值
COUNT:计数
LIMIT:起始行(从0开始) 查询几行
默认顺序按照插入顺序排序!
WHERE不支持聚合函数,HAVING支持!
ORDER BY:
ASC正序
DESC倒序!
GROUP BY:分组
HAVING:可以加聚合函数的筛选器,放在GROUP BY后面!
SELECT * FROM STUDENT LIMIT 1,2
//LIMIT分页查询数据(做程序内分页时可能用到)!
SELECT * FROM STUDENT ORDER BY SID ASC
SELECT * FROM STUDENT ORDER BY SID DESC
//ORDER BY正序/倒序查询!
SELECT * FROM STUDENT ORDER BY JSP ASC,HTML DESC
//主顺序与副顺序,副顺序只在主顺序内起作用!
SELECT SEX,COUNT(*) FROM STUDENT GROUP BY SEX HAVING SEX IS NOT NULL
SELECT SEX ,COUNT(*) FROM STUDENT GROUP BY SEX HAVING COUNT(*)>2
//HAVING筛选器可以加聚合函数!
以上是关于2018-07-10聚合函数+比较条件+''和NULL+DISTINCT+ORDER BY+LIMIT+GROUP BY的主要内容,如果未能解决你的问题,请参考以下文章