MySQL查询语句

-------------------------------------------------------------------------------
与统计函数配合使用

统计函数:

select 属性1,属性2,count(属性3) from 表名 group by 属性3;
having

select 属性1,属性2,属性3 - 属性4 as s from 表名 where 属性 = 1 having s > 200;
select 属性1 SUM(属性2 * 属性3) as hk from 表名 group by 属性1 having hk >200

思路:
#所有人的平均分
select name ,avg(scop) from stu group by name
#每个人的挂科情况
select name,scop<60 from stu
#计算每个人的挂科科目
select name,sum(scop<60) from stu group by name;
#计算出每个人的挂科数 和平均分
select name,sum(scop<60) ,avg(scop) from stu group by name
#平均分低于60分的
select name,sum(scop<60) as gk,avg(scop) as p from stu group by name having gk >=2;
最终查询出的结果:

====================================================================================

# select 属性1,属性2,属性3 from 表名 [where 属性>1] ORDER BY 属性1 [desc\asc],属性2 [desc\asc]
# limit[offset] N offset:偏移量 n:取出几个:例如:limit 3 如同:limit 0,3
select * from stu ORDER BY scop desc limit 1,3
#5个字句有顺序要求:where 、group by、 having、 order by、 limit
==========================子查询=======================================

where 类型的子查询:把内层查询的结果作为外层查询的比较条件
解决:查询最大商品,最贵的商品
例如:
#查询出最大的ID
select max(id) from goods;
#将 where id = 设为动态变量即可
select id,goods_name from goods where id = "最大的ID值";
#合并版本:
select id,goods_name,shop_price from goods where id = (select max(id) from goods)
# 查询出相同cat_id的最贵的商品
select cat_id,goods_name,shop_price from goods where shop_price IN( select max(shop_price) from goods group by cat_id )

=====================================
From型的子查询:把内层的查询结果,当成临时表,供给外层SQL再次查询。
解决:查询每个栏目下的最新、最贵的商品
例如:
select cat_id,goods_name,shop_price from
(select id,cat_id,goods_name,shop_price from goods order by cat_id asc,shop_price desc)
as tmp group by cat_id;
#用子查询 挂科 以及挂科两门以上同学的成绩
# 挂科2门以及以上的人找出来
select name,count(*) as gk from stu where scop < 60 group by name having gk >=2

#去掉多余的gk列
select name from ( select name,count(*) as gk from stu where scop <60 group by name having gk>=2) as tmp

#最终版 子查询 挂科 以及挂科两门以上同学的成绩
select name,avg(scop) from stu where name in(select name from ( select name,count(*) as gk from stu where scop <60 group by name having gk>=2) as tmp) group by name;

Exists子查询:把外层的查询结果,拿到内层,看内层的查询是否成立
解决:查询所有商品的栏目

第二个表:


Exists子查询:把外层的查询结果,拿到内层,看内层的查询是否成立
select cat_id,cat_name from category where exists ( select * from goods where goods.cat_id = category.cat_id);

例如:


select * from category where EXISTS ( select * from goods ) 结果:

select * from category where EXISTS ( select * from goods where category.cat_id = goods.cat_id) 结果:

select * from goods where EXISTS ( select * from category where category.cat_id = goods.cat_id)

总结:



=======================unino:(联合)合并查询结果:将两次或者多次的查询结果合并==================



unino语法:select * from tableName where 属性 > 5000 unino select 属性 from tableName 属性 < 20;

===========================================
unino:将两次或者多次查询的结果进行合并
要求:两次查询的列数一致。多张表unino时,多次SQL语句取出来的列名可以不一致,此时已第一个SQL的列名为准。
推荐:查询的每一列,相对应的



union查询,将两个Select语句合并为一个语句:

========================================
create table ta
(
id char(1),
num int
);
insert into ta values
('a',5),
('b',10),
('c',15),
('d',10);
create table tb(
id char(1),
num int
);
insert into tb values
('b',5),
('c',10),
('d',20),
('e',99);



要求:
如果某一行数据在 ta 或者 tb 表中有 就已 ta 或者tb表的数据为准
如果两个表都有同一行数据,就把两个表里的相同数据相加

==================================================================
union all:如果不同的语句中取出的行,有完全相同(每个列名相同,并且列的值也相同),那么相同的行将会合并(去重复),如果不去重复复,可以家all来指定

错误的演示:

正确的写法:

如果字句中有order by,limit,必须加(),推荐放到所有字句的后面,---对最终结果进行排序


在字句中,order by 配合limit使用才有意义,如果order by 不配合limit使用,会被语法分析器优化分析时,去除

======================================================================================
++++++++++++++++++++++++++++++++++++++连接查询+++++++++++++++++++++++++++++++++++++++++
======================================================================================


笛卡尔积:两个集合相乘





左(left)连接:
select 列1,列2,列N from tableNameA left join tableNameB on tableNameA.属性 = tableNameB.属性(关系型数据库存在的"关系") where,group by,having...语法一致
右(rigth)连接
select 列1,列2,列N from tableNameA rigth join tableNameB on tableNameA.属性 = tableNameB.属性(关系型数据库存在的"关系") where,group by,having...语法一致
内(inner)连接
select 列1,列2,列N from tableNameA inner join tableNameB on tableNameA.属性 = tableNameB.属性(关系型数据库存在的"关系") where,group by,having...语法一致
内连接:查询左右表的数据,即:不要左/ 右 表中的 null的那一部分,内连接是左连接和右连接的交集。



左右连接:以左表(left)为准,去右表找匹配的数据,找不到匹配,用Null补齐。
如何记忆:
1.左右连接可以相互转化。
2.可以把右连接转化为左(left)连接来使用(推荐使用左连接,不同的数据库兼容性好一些)
区别:





浙公网安备 33010602011771号