MySQL数据库查询
表中数据
1、模糊查询
like子句:在where子句中,可以使用该字句以及关键字结合实现模糊查询
select * from 表名 where 字段名 like '关键字';
通配符
% : 表示匹配0个或者多个字符(NULL除外)
_ : 表示匹配任意一个字符(多个_ , 就匹配对应个数)
可以在通配符前后指定文字
select * from users where LoginPWD like '0%';
select * from users where LoginPWD like '__AO';
select * from users where LoginPWD like '%00%';
2、消除重复项
对查询的结果去重 —— distinct
select distinct * from 表名;
select distinct LoginPWD from users;
select distinct LoginPWD from users;
select distinct BloodTypeID from users;
3、排序
在查询中添加排序 ,
select * from 表名 order by 字段名 desc;
默认为升序
asc :按照指定的字段升序排序(默认)
desc:降序排序
select * from users order by StarID;
select * from users order by StarID desc;
select * from users where StarID>4 order by BloodTypeID;
select * from users order by BloodTypeID desc;
中文排序
关键字:CONVERT
# 语法: select 检索列 from 表名 order by convert(中文数据的列名 using "GBK") 规则;
按照名字升序
select * from users order by convert(NickName using "GBK");
按照名字降序
select * from users order by convert(NickName using "GBK") desc;
排序限制数量limit
select * from users order by convert(NickName using “GBK”) limit 3;
4、分组
在查询中添加排序 ,
select * from 表名 group by 字段名;
select * from users group by BloodTypeID;
如果一条sql语句中同时出现了多个子句 摆放顺序如下
from > where > group by > having > order by > limit
having 同时表达