将查询到的结果按照升序降序排列出来
SELECT
*
FROM
products
WHERE
id < 8
ORDER BY
price DESC,
pnum ASC; //价格相同按数目排序。asc升序 ,desc降序
聚合函数的查询
掌握5个 count sum max min avg
SELECT
MAX(price)
FROM
products;
SELECT
MIN(ifnull(price, 0))
FROM
products;
重点:if null(price,0).如果最小值为空,显示0
将查询结果去重显示
SELECT DISTINCT
category
FROM
products;
对查询结果进行分组
SELECT
sum(pnum)
FROM
`products`
GROUP BY
category
HAVING
sum(pnum) > 100;
对查询结果进行分页
SELECT
*
FROM
products
LIMIT 0,
4;
从第一个开始显示(0是第一个),每页显示4条数据
sql查询语句知识点总结
查询中用到的关键词有6个,书写顺序为
select-from-where-group by-having-order by
select from 是必须的.关键字执行顺序为
from-where-group by-having-select-order by
关键字作用
from :找到要查询的表
where:判断条件,进行筛选
group by:对记录按照指定列进行分组
having:对分组后的信息进行筛选
select:选择需要的列信息
limit:分页。从哪个开始显示,每页显示几个
order by:对查询信息进行排序