2012-03-13 73 views
0

我正在尋找一個MySQL查詢(使用PHP)在時間去通過每個表一個,只顯示不匹配的關鍵日期的所有結果在t_lastmod表中。我不知道如何說ON表名=字段值。SQL JOIN表名上,等於字段值

t_one

guid | name | lastmod 
1 | Joe | 2012-01-01 01:00:00 
2 | Tom | 2012-01-02 01:00:00 
3 | Sue | 2012-03-01 02:00:00 

t_two

guid | pet | lastmod 
4 | cat | 2012-01-01 01:00:00 
5 | dog | 2012-01-02 01:00:00 
6 | fish | 2012-03-01 02:00:00 

t_three

guid | fruit | lastmod 
7 | orange | 2012-01-01 01:00:00 
8 | pear | 2012-01-02 01:00:00 
9 | grape | 2012-03-01 02:00:00 

t_lastmod

table_name | lastmod 
    t_one | 2012-01-01 01:00:00 
    t_two | 2012-01-02 01:00:00 
    t_three | 2012-01-01 02:00:00 

查詢結果是:

t_one => 2 | Tom | 2012-01-02 01:00:00 
t_one => 3 | Sue | 2012-03-01 02:00:00 
t_two => 4 | cat | 2012-01-01 01:00:00 
t_two => 6 | fish | 2012-03-01 02:00:00 
t_three => 8 | pear | 2012-01-02 01:00:00 
t_three => 9 | grape | 2012-03-01 02:00:00 

到目前爲止我的代碼(需要在JOIN t_lastmod上的幫助...)

$tables = array('t_one', 't_two', 't_three'); 

    foreach ($tables AS $table) { 

    $query = " select $table.* from $table JOIN t_lastmod ON $table = t_lastmod.TABLE_NAME WHERE $table.lastmod != t_lastmod.lastmod "; 

    } 
+0

查詢的當前輸出是什麼? – 2012-03-13 00:21:33

回答

1
select $table.* 
from $table 
JOIN t_lastmod ON '$table' = t_lastmod.TABLE_NAME 
WHERE $table.lastmod != t_lastmod.lastmod " 
+0

在ON子句中將單引號括在$表中。 – Vikram 2012-03-13 00:25:10

+0

謝謝。那很簡單 :) – Jeffrey 2012-03-13 00:45:14