mysql如何实现跨数据库查询并按where子

mysql如何实现跨数据库查询并按where子,第1张

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);

如果是mysql里面两个不同的数据库,应该是可以直接使用 [数据库名称][表名]来关联的。TP指定的数据库,是因为他要缓存这个数据库的表字段等。试试看行不行,测试通过: SELECT a,b FROM table1 a LEFT JOIN db2table2 b ON aid=bid

以上就是关于mysql如何实现跨数据库查询并按where子全部的内容,包括:mysql如何实现跨数据库查询并按where子、tp5如何跨数据库查询、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10191471.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存