MySQL之查詢多個相同表結構的數據


項目中數據量比較大需要每個月分一次表,按照時間分表;

分表查詢邏輯:先按條件查詢出要查詢的表,再去多張表里面查詢符合條件的數據

MySQL查表語句:

// 這種方式不支持條件查詢
show tables;

// 這種方式可以根據table_name字段匹配符合條件的表
select * from information_schema.tables where table_name like 'th%';

 

分出來的表一定是表結構相同的數據表,多張相同數據結構的表查詢語句:

// union all 關鍵字查詢
select * from thermal_oil_pipeline_202009060553 p
union all
select * from thermal_oil_pipeline_202009060553 p
union all
select * from thermal_oil_pipeline_202009060553 p

 

分頁查詢會用到符合條件數據總數量這個數據,分表查詢符合條件數據總量sql如下:

select sum(count) from (
	select count(p.id) as count from thermal_oil_pipeline as p 
	where  equipmentNo='FIC4' and createTime >= '2020-09-02 14:30:44' and createTime <= '2020-09-06 14:30:44'
	union all select count(p.id) as count from thermal_oil_pipeline_202009060553 as p 
	where  equipmentNo='FIC4' and createTime >= '2020-09-02 14:30:44' and createTime <= '2020-09-06 14:30:44'
) as total;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM