select 字段 from 表名 where 条件; # 例: select * from test_table where id > 2; # 筛选出id大于2的所有字段
# 例: select * from test_table where id = 2; # 筛选出id等于2的所有字段 select * from test_table where id > 2; # 筛选出所有id大于2的所有字段 ....
# 例: select * from test_table where id > 2 and age<18; # 筛选出所有id大于2并且年龄小于18的所有字段 select * from test_table where id > 2 or age<18; # 筛选出所有id大于2或者年龄小于18的所有字段 select * from test_table where not(id > 2 and age<18); # 筛选出所有不是id>2并且年龄小于18的字段 ....
# 例: select * from test_table where name like "小%" # 查询数据库中小开头的所有信息的所有字段 select * from test_table where name like "小_" # 查询数据库中所有小开头的两个字的所有字段 select * from test_table where name rlkie ".." # 查询数据库的两个字符的所有数据的所有字段
# 例 select * from test_table where id in (1,3,5) # 查询id编号为1,3,5的所有字段信息 select * from test_table where between 1 and 5 # 查询id编号为1到5之间的所有数据的所有字段信息
# 例: select * from students where text is null; # 筛选出text字段为空的数据 select * from students where text is not null; # 筛选出text字段为不为空的数据
这里需要使用到聚合函数,聚合函数有
# 例: select count(*) from test_table; # 可以直接得到一共有多少个数据 select max(id) from test_table; # 可以直接得到id字段中最大的id值为多少 select * from test_table where id = (select max(id) from test_table); # 可以直接使用聚合函数查询最大id的所有的字段信息
将数据按照字段进行分组,可以使用聚合函数对分组数据进行统计,关键字group by
# 例: alter table test_table add tab int not null default 0; # 创建一个新的字段tab 默认为0 select tab from test_table group by tab; # 按照tab进行分组 # 分组后筛选数据 select tab, count(*) from test_table group by tab having tab=0;
语法:
select * from 表名 order by 字段1 asc|desc,字段2 asc|desc,...
关键字:order by
asc是从小到大排序、即升序
desc是从大到小排序、即降序
# 例: select * from test_table order by id asc # 按照id从小到大排序 select * from test_table order bt id desc # 按照id 从大到小排序
关键字:limit
start:开始的索引
count:取多少条数据
select * from 表名 limit start, count # 例: select * from test_table limit 0,5 # 去前5个数据
如对本文有疑问, 点击进行留言回复!!
MySQL-关系代数-并、交、差、等值连接、自然连接、左连接。。。
网友评论