1.检索不同的行
查询数据,过滤指定列名重复的数据,在列名前面添加 distinct 关键字
例:select distinct vend_id from products
2.限制结果
查询数据,指定查询开始行和查询数量,使用 limit 关键字
-例:select vend_id from products limit 5 //第五行开始
-例:select vend_id from products limit 5,4 //第五行开始,查询4行
3.排序查询
查询数据,指定列名进行排序,使用 order by 关键字
-例:select * from products order by id //以id进行排序
-例:select * from products order by id,price //在id排序后的基础上以price排序
*可使用 desc 设置降序查询,asc 设置升序查询[默认排序方式
4.WHERE检索
-例:select * from products where id <> 1
-例:select * from products where id between 5 and 10
-例:select * from products where price is null //查询无值数据
5.WHERE其他子句
AND操作符、OR操作符、IN操作符、NOT操作符
-例:select * from products where id in(5,6) //检索id等于5、id等于6的数据
-例:select * from products where id not in(5,6) //取反
6.LIKE操作符
百分号(%)通配符,'jet%'是以jet开头的数据、下划线通配符,'_ ton anvil'同理,单字符检索
-例:select * from products where id like 'jet%'
-例:select * from products where id like '_ ton anvil' //.5 ton anvil不符合要求
7.正则表达式
使用 regexp 关键字,按照要求进行查询数据,与like不同,只要文本中包含查找的数据就匹配。
-例:select * from products where id regexp '.00' //.表示匹配任意一个字符
-例:select * from products where id regexp '100|200' // |表示or
-例:select * from products where id regexp '[123] TON' // [123]等同于 ‘1|2|3’,[^123]代表除这些字符意外的
-例:select * from products where id regexp '[0-9] TON' // [0-9]等同于[0123456789],也可以写字母[a-z]
-例:select * from products where id regexp '[0-9] TON' // [0-9]等同于[0123456789],也可以写字母[a-z]
-例:select * from products where id regexp '\\.' // \\表示查询特殊字符,\\.查询.。
-例:select * from products where id regexp '\\\' // \\\查询\。
-例:select * from products where id regexp '\\([0-9] sticks?\\)' //查询小括号,0-9的数字,可有可无的s,如stick或sticks
-例:select * from products where id regexp '[[:digit:]]{4}' //查询4位数字
-例:select * from products where id regexp '^[0-9\\.]' //匹配开始第一位为数字或.。
8.拼接字段
将两个字段的信息合并为一个字段显示,使用 Concat() 关键字
例:select Concat(vend_id,'(', vend_name,')') from products //输出 1(汪强)
9.字段别名(导出列)
使查询的字段名显示为指定的名称,使用 as 关键字
例:select Concat(vend_id,'(', vend_name,')') as vend_title from products //字段名为 vend_title