高级查询

一、where条件查询

1.1 where基本查询

select * from 表 where id > 1
select nid,name,gender from 表 where id > 1

select * from 表名 where 列名 = 值;  # 查询列名为这个值的所有行的数据
select * from 表名 where 列名 > 值;  # 查询列名值大于这个值的所有行的数据

注意:where之后可以跟的运算符类型如下

1.2 between and查询(闭区间)

select * from t1(表名) id(列名) between 5(值5) and 16(值16);
select * from 表 where id between 5 and 16;
# 查询的是id值在1和4中间的所有行的数据,并且这个5到16是闭区间

1.3 in查找(集合里查找)

select * from t1(表名) id in (1,2,5);
# 查询的是id值是1,2,5的行的所有数据

select * from 表 where id in (11,22,33)
select * from 表 where id not in (11,22,33)
select * from 表 where id in (select nid from 表)

1.4 通配符(%,_)查找,又称模糊查找

select * from 表 where name like 'inc%' - inc开头的所有(多个字符串)
select * from 表 where name like 'inc_' - inc开头的所有(一个字符)

# 例如我要找一个increment ,但我只知道他是in开头想要查找就需要用到通配符查找

select * from t1(表名) where name(列) like('in%') # 可以查找到,%可以代表任意长度字符 select * from t1(表名) where name(列) like('in_') # 不可以找到,_只能代表一个字符

二、limit(限制取几条)

limit [偏移量], 取出条目
分页应用中最为典型,如第1页取1-20条,第2页取21-40条

select * from 表名 索引偏移量,取出数据量(offset);
# 表示的是一次显示多少条数据,这也是网页分页的核心SQL

select * from 表 limit 5;             # 前5行
select * from 表 limit 4,5;          # 从第4行开始的5行
select * from 表 limit 5 offset 4   # 从第4行开始的5行(不推荐)

三、order by (排序)

对栏目的商品按价格由高到低或由低到高排序
各种排序场合,如取热点新闻,发帖状元等
默认是升序排列

# 降序
select * from t1 order by id(列名) desc;
# 升序
select * from t1 order by id(列名) asc;

# 多列排序
select * from t1 order by id(列名) desc,num(列名) asc;  # 多列用逗号隔开
# 先按前一列进行排序,如果第一列排完有值相同的,再根据第二个规定的列进行排序
# 也就是如果第一个列排完了,并且没有重复,那就没有第二个排序的事了

四、group by(分组)

把行按某个字段进行分组
见于统计场合,如按栏目计算帖子数,统计每个人的平均成绩等

select num from 表 group by num
select num,nid from 表 group by num,nid
select num,nid from 表  where nid > 10 group by num,nid order by nid desc
select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
select num from 表 group by num having max(id) > 10
# 特别的:group by 必须在where之后,order by之前


# 聚合函数可以根据聚合函数可以对分组里的内容进行一些操作(求个数,求和...) 需要传如一个列名,他根据这个列名操作。
select name(列), 聚合函数(count(),sum(),max(),min(),avg())from t1(表名) group by age(列名);


# 二次筛选: 针对的是一次查询之后的结果,还需要进一步删选出符合条件的结果
select name,count(name) as c from t1 group by age having c>3;
# 先是根据年龄进行了分组,并且第一次选出结果  as 是起别名
# having是对第一次筛选的结果做出个数大于3的二次筛选

having和where的异同点

  • having与where类似,可筛选数据
  • where针对表中的列发挥作用,查询数据
  • having针对查询结果中的列发挥作用,筛选数据
# 注意:上述查询条件写的时候的顺序
    group by > order by > limit

select name,sum(score) from 表 where id > 10 group by score order by age desc limit 2, 10

五、连表查询

select * from 表1,表2;    # 会由于笛卡尔积的关系,显示出的不是我们想要的结果
# 要想得到想要的结果,必须确定外键的对应关系
select * from t1,t2 where t1.t2_id = t2.id; # 把t1的外键 和t2的主键条件判断一下

5.1 作用

从2张或者多张表中,取出有关的数据

5.2 场景

取文章及所在栏目名称,取个人信息及所发布的文章等

5.3 种类

左连接

有连接

内连接

5.4 用法

5.4.1 (left join)左连接

A表所有显示,如果B中无对应关系,则值为null

select A.num, A.name, B.name from A left join B on A.nid = B.nid

5.4.2 (right join)右连接

B表所有显示,如果B中无对应关系,则值为null

select A.num, A.name, B.name from A right join B on A.nid = B.nid

5.4.3 (inner join)内连接

内连接是左右连接的交集

select A.num, A.name, B.name from A inner join B on A.nid = B.nid

六、子查询

子查询就是在原有的查询语句中,嵌入新的查询,来得到我们想要的结果集。

6.1 where型子查询

把内层sql语句查询的结果作为外层sql查询的条件

6.1.1 典型用法

select * from tableName 
where colName = (select colName from tbName where ....) 
或 {where colName in (select colName from tbName where ..)}

6.1.2 应用场景

查询出某大栏目下的所有商品

 

七、最终顺序

select * from 表 (where 条件) (group by 列名 [having 二次筛选]) (order by 列名) (limit 索引偏移,offset);
# 括号只是表明查找的方法,[]代表可有可无,实战中是没有括号的

 

posted on 2019-10-01 16:52  软饭攻城狮  阅读(130)  评论(0编辑  收藏  举报

导航