SQL注入:select基本使用
select和条件查询
select * from tableName where [筛选条件1] or|and [筛选条件2] ... ;
select排序、限制和子查询
排序
#默认升序排列:
select * from tableName order by columnName;
#降序排列
select * from tableName order by columnName desc;
#指定根据第几个字段进行排序
select * from tableName order by 1|2|3|4...;
限制查询结果条目个数
#根据第一个字段进行排序,并且返回前两行数据内容
select * from tableName order by 1 limit 2;
#根据第一个字段进行排序,并且返回第二个数据开始,前两个查询内容
select * from tableName order by 1 limit 1,2;
子查询
select * from tableName where columnName in (select columnName from tableName2);
select * from tableName a where columnName exists(select columnName from tableName2 b where a.xx=b.xx);
1、exists是对外表做loop循环,每次loop循环再对内表(子查询)进行查询,那么因为对内表的查询使用的索引(内表效率高,故可用大表),而外表有多大都需要遍历,不可避免(尽量用小表),故内表大的使用exists,可加快效率;
2、in是把外表和内表做hash连接,先查询内表,再把内表结果与外表匹配,对外表使用索引(外表效率高,可用大表),而内表多大都需要查询,不可避免,故外表大的使用in,可加快效率。
内链接
select a.xx,b.zz from tableName1 a inner join tableName2 b on a.ww=b.ww;
左链接(左表为主表,全部显示)
select a.xx,b.zz from tableName1 a left join tableName2 b on a.ww=b.ww;
右链接(右表为主表,全部显示)
select a.xx,b.zz from tableName1 a right join tableName2 b on a.ww=b.ww;

浙公网安备 33010602011771号