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


表结构相同,分表后如何查询所有表的数据?可以使用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的同时使用。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM