Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL查询交叉引用_Php_Mysql_Sql_Join - Fatal编程技术网

Php MySQL查询交叉引用

Php MySQL查询交叉引用,php,mysql,sql,join,Php,Mysql,Sql,Join,我正在寻找一个mysql查询(使用php)一次遍历每个表,只显示与cross_check.lastmod中不同的结果 t\u 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 guid | fruit | lastmod 7 | orange | 2012-01-01 01:00

我正在寻找一个mysql查询(使用php)一次遍历每个表,只显示与cross_check.lastmod中不同的结果

t\u 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
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\u 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_三

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
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
交叉检查

guid | lastmod
   1 |  2012-01-01 01:00:00
   2 |  2012-01-02 01:00:00
   3 |  2012-01-01 02:00:00
   4 |  2012-01-01 01:00:00
   5 |  2012-01-02 01:00:00
   6 |  2012-01-01 02:00:00
   7 |  2012-01-01 01:00:00
   8 |  2012-01-02 01:00:00
   9 |  2012-01-01 02:00:00
查询结果将是:

t_one => 3 | Sue   | 2012-03-01 02:00:00
t_two => 6 | fish  | 2012-03-01 02:00:00
t_three => 9 | grape | 2012-03-01 02:00:00
到目前为止我的代码(需要表格交叉引用)


尝试使用此查询,当然要替换foreach循环中的表名

SELECT origin.guid as guid FROM t_one as origin, cross_check as cross WHERE cross.lastmod != origin.lastmod;

尝试使用此查询,当然要替换foreach循环中的表名

SELECT origin.guid as guid FROM t_one as origin, cross_check as cross WHERE cross.lastmod != origin.lastmod;

只需在
交叉检查表中加入
JOIN

SELECT t_one.*
FROM t_one
JOIN cross_check ON t_one.guid=cross_check.guid
WHERE t_one.lastmod != cross_check.lastmod
尽管如此,每个表仍然有一个查询(您可以随意使用联合,但这不会减少查询的核心数量)

如果要将它们合并为一个,可以使用联合:

[query for t_one]
 UNION
[query for t_two]
 UNION
[query for t_three]
我觉得这并不比做三个不同的查询更有效(不过这是一种直觉,未经证实的说法)

您也可以这样做(虽然这只在
t*
guid
s之间没有交叉时起作用;如果有
t\u一个
,那么
t\u二个
):


如果你想抓住标签而不是ID,你也可以把
COALESCE(t_one.name,t_two.pet,t_three.fruit)作为标签。
只要
交叉检查表中加入

SELECT t_one.*
FROM t_one
JOIN cross_check ON t_one.guid=cross_check.guid
WHERE t_one.lastmod != cross_check.lastmod
尽管如此,每个表仍然有一个查询(您可以随意使用联合,但这不会减少查询的核心数量)

如果要将它们合并为一个,可以使用联合:

[query for t_one]
 UNION
[query for t_two]
 UNION
[query for t_three]
我觉得这并不比做三个不同的查询更有效(不过这是一种直觉,未经证实的说法)

您也可以这样做(虽然这只在
t*
guid
s之间没有交叉时起作用;如果有
t\u一个
,那么
t\u二个
):

如果你想抓住标签而不是ID,你也可以把
COALESCE(t_one.name,t_two.pet,t_three.fruit)作为标签