首页 > 数据库技术 > 详细

MySQL分表后查询所有表中的记录

时间:2021-08-26 11:29:45      阅读:29      评论:0      收藏:0      [点我收藏+]

表结构相同,分表后如何查询所有表的数据?可以使用union关键字

select * from table1 where type=1 union
select * from table1 where type=1 union
select * from table3 where type=1 union
select * from tablen where type=1

上述代码是对每个分表进行过滤后再连接。当然也可以先连接,再过滤数据:

select * from (
    select * from table1 union
    select * from table1 union
    select * from table3 union
    select * from tablen 
) t
where t.type=1
order by t.id desc

需要注意的是,对于排序和分组,只能在外部进行,不能在使用union的同时使用。

MySQL分表后查询所有表中的记录

原文:https://www.cnblogs.com/zys2019/p/15177330.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!