简体   繁体   English

SQL JOIN在表名称等于字段值上

[英]SQL JOIN ON a table name equal to a field value

I'm looking for a mysql query (using php) to go through each table one at a time and only display all results that don't match the date of the key in the t_lastmod table. 我正在寻找一个mysql查询(使用php)来一次遍历每个表,并且仅显示与t_lastmod表中的键日期不匹配的所有结果。 I'm not sure how to say ON tablename = field value. 我不确定如何说ON表名=字段值。

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 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 t_三

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 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

The query results would be: 查询结果将是:

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

My code so far (need help on the JOIN t_lastmod ON...) 到目前为止,我的代码(在JOIN 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 ";

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM