2012-03-13 4 views
0

mysql 쿼리 (php 사용)를 사용하여 각 테이블을 한 번에 하나씩 찾고 키의 날짜와 일치하지 않는 모든 결과 만 표시합니다. t_lastmod 테이블에서. 나는 tablename = field 값에 대해 어떻게 말할 지 모르겠습니다.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 

내 코드를 지금까지합니다 (t_lastmod ON 가입에 대한 도움이 필요합니다 ...)

$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

쿼리의 현재 출력은 무엇입니까? –

답변

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

ON 절에 $ table 주위에 작은 따옴표를 넣으십시오. 덕분에 – Vikram

+0

. 그것은 쉽다 :) – Jeffrey