查询语句:
select `输出` from `获取数据` where `过滤` group by `分组`
having `过滤` order by `排序` limit `限定个数`;
语句按照以下顺序执行:
from、where、group by、select、having、order by、limit
如果有表格class,内容如下:
id | name | class_id |
---|---|---|
1 | a | 1 |
2 | b | 1 |
3 | c | 2 |
4 | d | 2 |
5 | e | 1 |
6 | f | 1 |
7 | g | 2 |
对表格class执行语句:
select * from class group by class_id;
得到以下结果:
id | name | class_id |
---|---|---|
1 | a | 1 |
3 | c | 2 |
对表格class执行语句:
select id,class_id from class where id %2 = 0 group by class_id;
得到以下结果:
id | class_id |
---|---|
2 | 1 |
4 | 2 |