数据查询
一、select语句
select [all | distinct] #默认all,所有数据保留
字段[……n] #username as 用户名,多个字段
from #from 进行 列 筛选
表名[……n] #多表为链接查询
[where 条件] #where进行 行 筛选 对原始数据进行筛选
[group by 列名[……n]] #对查询的数据进行分组
group by uCity; '''查询表后,按uCtiy分组'''
[with rollup] #关键字指定的结果集更详细
[having 条件] #指定分组后的数据进行筛选 对分组进行筛选
[order by 列名 #对查询的数据进行排序
[asc | desc] [……n]] #desc为降序 默认升序
[limit <子句>] #用于限制select查询语句返回的行数
limit 3,3; '''第四行开始,显示三行'''
[union <运算符>] #将多个查询结果组合为一个结果集
union
select uid,uname,usex; '''合成一个表'''
二、select [all | distinct]
select [all | distinct] #默认all,所有数据保留 ,distinct删除重复项
字段[……n] #username as 用户名,多个字段 ,as命名
from #from 进行 列 筛选
表名[……n] #多表为链接查询
#案例
select distinct gdcity
from goods
where gdprice>200
limit 3; #返回行数
三、where选择行语句
1.00 #使用逻辑用符,默认为 无
where [not|and|or] 表达式1 逻辑运算符 表达式2
#and用符链接
where year(ubirth) >= 220 and usex = '男';
2.00 #使用between and 运算符
where 表达式 [not] between 初始值 and 终止值
# 查询goods表中gdprice 在100到500的商品名称和价格
where gdprice between 100 and 500;
3.00 #使用 in 运算符 //使用 in 运算符 相当于 好几个 or 表达式
where 表达式 [not] in (值1,值2……值n)
# 查询goods表中gdcity为长沙,西安城市的商品
where gdcity in ('长沙','西安');
4.00 #使用 like 运算符 // like 可查询字符串
where 列名 [not] like '字符串' [escape '转义字符']
# 查询users表中 uname 为 '李' 开头的用户姓名、性别和手机号
where uname like '李%';
---------------------------------------------------------
% 任意字符串 s% 表示查询以S为开头的任意字符串
%S 表示查询以s结尾的任意字符串
%s% 表示查询包含s的任意字符串
_ 任何单个字符串 _s 查询以s结尾且长度为2的字符串
s_ 查询以s开头为两个字符的字符串
---------------------------------------------------------
5.00 #使用 is null 运算符 当未给表中的列提供数据值时,系统自动将其设置为空值
where 列名 is [not] null
# 查询users表中ulmage为空的用户名和性别
select uname,sex,uimage from users
where uimage is null;
lmage为空的用户名和性别
select uname,sex,uimage from users
where uimage is null;
---
### 四、