从多个表中检索最大n个ID [英] retrieve the max n ids from multiple tables

查看:76
本文介绍了从多个表中检索最大n个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有



i需要在7张桌子中返回最新添加的3行



所以我从id desc

订购的每个表中检索顶部(1)结果我从每个表中得到7个顶行



然后我必须拍摄来自7 ids的最高3 ids我有它



我怎么能归还这3个ids ??

推荐答案

尝试使用 union http://www.w3schools.com/sql/sql_union.asp [ ^ ]



try with union http://www.w3schools.com/sql/sql_union.asp[^]

select top 3 id from 
   (select top 3 id from tbl1
     union
     select top 3 id from tbl2
     union
     select top 3 id from tbl3
     union
     select top 3 id from tbl4
     union
     select top 3 id from tbl5
     union
     select top 3 id from tbl6
     union
     select top 3 id from tbl7
     order by id desc) X
order by id desc


这篇关于从多个表中检索最大n个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆