字段命名別名 關鍵字 as ,as之前為待命名的表或字段,之后為命名別名,其中 as 可以省略
select y_name as 姓名 from yingxiong
image.png
求最大最小值 關鍵字 max(查詢字段) min(查詢字段) 需要注意的一點是
查詢字段類型必須是int
如果是varchar 可以通過max(查詢字段+0)來修正
SELECT max(y_age) from yingxiong
image.png
求平均值 關鍵字 avg
SELECT avg(y_age) from yingxiong
image.png
求和 關鍵字 sum
SELECT sum(y_age) from yingxiong
image.png
求總數 關鍵詞 count
SELECT count(y_age) from yingxiong
image.png
子條件查詢
select * from yingxiong where y_age in(SELECT y_age from yingxiong where y_type='中單')
#篩選類型為中單的角色的年齡,并列出是這些年齡的所有字段
image.png
分組查詢一般伴隨著常用函數一起使用 關鍵詞 group by 表示分組,
按照某一個字段的值進行分組
SELECT y_type,avg(y_age)from yingxiong GROUP BY y_type
#用類型Type來分組 查詢不同Type的平均年齡
image.png
分組條件查詢 關鍵詞 having 是分組中的條件關鍵字 (類似where)
按照某一個字段的值進行分組
SELECT y_type,sum(y_age) as ageHE from yingxiong group by y_type having ageHE>'60'
image.png