碎片时间杂学 6.14 数据库基础补漏

别名:

select abc AS a,def AS d from paramtable where a = ‘test’;

查询常数:

SELECT '王者荣耀' as platform, name FROM heros

去除重复行:

SELECT DISTINCT attack_range FROM heros

排序检索数据:

SELECT name, hp_max FROM heros ORDER BY hp_max DESC

约束返回结果的数量:

SELECT name, hp_max FROM heros ORDER BY hp_max DESC LIMIT 5

SELECT 的执行顺序:

1. 关键词顺序(不能颠倒)

SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ...

2. 执行顺序

FROM > WHERE > GROUP BY > HAVING > SELECT的字段 > DISTINCT > ORDER BY > LIMIT

举例:

SELECT DISTINCT player_id, player_name, count(*) as num #顺序5

FROM player JOIN team ON player.team_id = team.team_id #顺序1

WHERE height > 1.80 #顺序2

GROUP BY player.team_id #顺序3

HAVING num > 2 #顺序4

ORDER BY num DESC #顺序6

LIMIT 2 #顺序7

你可能感兴趣的:(碎片时间杂学 6.14 数据库基础补漏)