Mysql准备做2014年度分区

时间:2013-11-19 17:36:08

标签: mysql sql partitioning information-schema

我需要获取尚未为2014分区的所有表的数据库和表的列表。 我在考虑查询满足以下条件的information_schemaPARTITIONS

PARTITION_NAME IS NOT NULL
PARTITION_NAME='p201312'
AND PARTITION_NAME !='p201401'

我怎么能查询这个。我试过了:

select DISTINCT * FROM(select `TABLE_SCHEMA`, `TABLE_NAME` from `information_schema`.`PARTITIONS` where PARTITION_NAME IS NOT NULL AND PARTITION_NAME='p201312' AND PARTITION_NAME!='p201401') as a;

但是不起作用。请帮忙。

*更新**

我能够整理一个查询以获得我需要的东西:

  

选择a。TABLE_SCHEMA,a。TABLE_NAME FROM(从TABLE_SCHEMA选择TABLE_NAMEinformation_schemaPARTITIONS,其中PARTITION_NAME ='p201312 ') 作为一个   LEFT JOIN(从TABLE_SCHEMATABLE_NAME选择information_schemaPARTITIONS,其中PARTITION_NAME ='p201401')为b   ON a。TABLE_NAME = b。TABLE_NAME WHERE b。TABLE_NAME为空;

有没有办法让这个查询更优雅?

1 个答案:

答案 0 :(得分:0)

select 
    table_schema, 
    table_name 
from 
    information_schema.tables t
where 
    not exists (
        select 
            null 
        from 
            information_schema.partitions p 
        where 
            p.table_schema = t.table_schema and 
            p.table_name = t.table_name and 
            p.partition_name like 'p2014%'
        )