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 name3、exists型子查询(把外层查询结果拿到内层,看内层的查询是否成立)#查询哪些栏目下有商品,栏目表category,商品表goodsselect cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id)如果A、B库在同一个mysql实例下(同一执行程序下)可以用A.search,B.room去访问:
update A.search s join B.room r on s.room_id=r.room_id
set s.is_online=r.is_online
不过你这room如果只有几条,直接把is_online对应room_id的值写在sql里去更新A.search表不就好了
update A.search set is_online=(case when room_id=x1 then y1 when room_id=x2
then y2 when room_id=x3 then y3 ...... else yn end)
评论列表(0条)