1检索数据
1).单列
select name –检索列的名字
from new_table; –检索来自哪个表
2)多列
select name**,**id –检索列的名字
from new_table; –检索来自哪个表
3)所有列
select * –检索列的名字
from new_table; –检索来自哪个表
4)不同的值
select distinct name –检索列的名字
from new_table; –检索来自哪个表
5)限制结果
select name –检索列的名字
from new_table –检索来自哪个表
limit 2;–输出前两列
2排序检索数据
1)按多个列排序
select name**,id ,**price –检索列的名字
from new_table –检索来自哪个表
order by price,id; (3,2) –第二列为price,第三列为id
2)指定排序方向
(1)降序
select name**,id ,**price –检索列的名字
from new_table –检索来自哪个表
order by price desc,id; (3,2) –第二列为price 降序排列,第三列为id
(2)升序
select name**,id ,**price –检索列的名字
from new_table –检索来自哪个表
order by price asc,id; (3,2) –第二列为price 降序排列,第三列为id
3过滤数据
1).单个值
select name,id –检索列的名字
from new_table –检索来自哪个表
**where**id<10;–输出id小于10的
2)不匹配
select name,id –检索列的名字
from new_table –检索来自哪个表
where name <> (!=) ‘张三’;–输出name不是张三的,数字可以不用加单引号
3)范围值
select name,id –检索列的名字
from new_table –检索来自哪个表
where id between 5 and 10;–输出id5到10之间的值
4)空值检查
select name,id –检索列的名字
from new_table –检索来自哪个表
where id is null;–输出id不存在的值
5)and或者or
select name,id –检索列的名字
from new_table –检索来自哪个表
where id <10 and(or) name !=’张三’;–输出id小于10并且(或者)名字不是张三的or
注:order by 语句应该放在where语句后面
or和and同时存在时,若先判断or则加括号区分
6)in操作符
select name,id –检索列的名字
from new_table –检索来自哪个表
where id in(5,10);–输出id5或10。与or功能相同,文字加单引号
6)not操作符
select name,id –检索列的名字
from new_table –检索来自哪个表
where not id=5;–输出id不等于5的值
4用通配符匹配
1)百分号
select name,id –检索列的名字
from new_table –检索来自哪个表
where name like ‘%y’;–输出y结尾的,不管前面有多少字符
2)下划线
select name,id –检索列的名字
from new_table –检索来自哪个表
where name like ‘_y’;–输出y结尾的,只匹配前面一个字符
3)方括号
select name,id –检索列的名字
from new_table –检索来自哪个表
where name like ‘[JK]%’;–输出J或K开头的。[JK]匹配括号中任意一个字符,若否定Wie[^JK]
5创建计算字段
1)拼接字段
select concat(name,’(‘,id,’)’ –name(id)
from new_table; –检索来自哪个表
2)去掉空格
select rtrim(name),’(‘,rtrim(id),’)’ –name(id)
from new_table; –检索来自哪个表
3)使用别名as
select name,id,age,id*age as new_id –创造新列
from new_table;–检索来自哪个表