【问题标题】:Query to return tables from mysql database not listed in a column in a table in the database查询以从 mysql 数据库返回未列在数据库表中的列中的表
【发布时间】:2014-11-29 03:45:45
【问题描述】:

我的 wordpress 数据库中有许多表(gooda814_bilby01)。我想要一个函数来将数据库中的表与数据库中表中的列中的值进行比较,以便该函数随后创建不在列表中的任何表。我有一个不优雅的 php 解决方案:

$fdc_tables = $wpdb->get_results('SELECT distinct `fdc_form` FROM `fdc_tables`', ARRAY_A);
$wp_tables =  $wpdb->get_results('show tables from gooda814_bilby01', ARRAY_A);
foreach( $fdc_tables as $fkey => $fvalue ) { $fdc[$fkey] = $fvalue['fdc_form']; }
foreach( $wp_tables as $wkey => $wvalue ) { $wpt[$wkey] = $wvalue['Tables_in_gooda814_bilby01']; }

$required tables = (array_values((array_diff($fdc, $wpt))));  

但我认为可能有一个整洁的 mysql 连接,例如:

select distinct fdc_forms from 
join show tables on Tables_in_gooda814_bilby01 = fdc_tables.fdc_forms

但是,这当然会返回错误 #1064

【问题讨论】:

    标签: mysql database wordpress join


    【解决方案1】:

    使用子查询而不是JOIN

    试试这个 SQL

    SELECT fdc_form 
    FROM fdc_tables 
    WHERE fdc_form NOT IN (
        SELECT T.TABLE_NAME
        FROM information_schema.TABLES T 
        WHERE T.TABLE_SCHEMA='gooda814_bilby01'
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多