1、where型子查询
#不用order by 来查询最新的商品
select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);
#取出每个栏目下最新的产品(goods_id唯一)
select cat_id,goods_id,goods_name from goods where goods_id in(select max(goods_id) from goods group by cat_id);
2、from型子查询
(把内层的查询结果供外层再次查询)
#用子查询查出挂科两门及以上的同学的平均成绩
思路:
#先查出哪些同学挂科两门以上
select name,count() as gk from stu where score < 60 having gk >=2;
#以上查询结果,我们只要名字就可以了,所以再取一次名字
select name from (select name,count() as gk from stu having gk >=2) as t;
#找出这些同学了,那么再计算他们的平均分
select name,avg(score) from stu where name in (select name from (select name,count() as gk from stu having gk >=2) as t) group by name;
3、exists型子查询
(把外层查询结果拿到内层,看内层的查询是否成立)
#查询哪些栏目下有商品,栏目表category,商品表goods
select cat_id,cat_name from category where exists(select from goods where goodscat_id = categorycat_id);
不能先select出同一表中的某些值,再update这个表(在同一语句中)
解决方案
1
2
3
4
5
6
7
--1把需要删除的数据放到另外的一张表里
create table table_test as select oneName from one
group by OneName,OneAge,oneSex,oneAddress having count(oneName) > 1;
--2删除不需要的数据
delete from one where onename in(select oneName from table_test);
--3删除创建的表
drop table table_test;
以上就是关于mysql如何实现跨数据库查询并按where子全部的内容,包括:mysql如何实现跨数据库查询并按where子、如何在MySQL数据库进行子查询、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)