个人总结工作上常用的SQL查询语句:
单表查询
1.简单查询
select * from table
2.查询表中某个字段
select t.xx from table t
3.条件查询
select t.xx from table t where t.id = ?;
3.根据某字段排序,降序/升序
select * from table t where t.id = ? order by t.xx desc/asc
4.分页
select * from table limit 0,100;//检索记录0到100行
select * from table limit 96,-1;//检索记录96到最后行
select * from table limit 100;//检索前100行
5.连表查询 left ,right ,inner
select * from tableA a left join tableB b on a.id = b.id
注意 关于mybatis中collection一对多关联查询分页出错问题总结
6. 分组查询
select * from table a where a.id = ? group by a.xx having count(id)>2;
注意分组查询时,其他字段只显示一个的问题