0

修改。我的 SQL 有限,非常感谢您的帮助。在一次点击中,我想通过 ID ($ga_id) 查询两个表,每个表都有一个内部连接到另一个表(表具有完全相同的列,并且父表彼此没有关系)。

我可以成功查询net_5_postmeta表如下

SELECT p.*
FROM net_5_postmeta AS pm
INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID 
WHERE pm.post_id = $ga_id
AND pm.meta_key = '_thumbnail_id' 
ORDER BY p.post_date DESC 
LIMIT 4

但我想查询 net_5_postmeta 表和 net_7_postmeta 表(它们具有相同的列),我认为它需要类似于...

SELECT p.*
FROM net_5_postmeta, net_7_postmeta AS pm
INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID
INNER JOIN net_7_posts AS p ON pm.meta_value=p.ID 
WHERE pm.post_id = $ga_id
AND pm.meta_key = '_thumbnail_id' 
ORDER BY p.post_date DESC 
LIMIT 4
4

1 回答 1

1

正如您所说的父表没有关系,我只能假设您想将57表视为相同。您应该使用union all将他们的数据放在一起,然后执行 ORDER BY/LIMIT。

SELECT *
FROM
(
  SELECT p.*
  FROM net_5_postmeta AS pm
  INNER JOIN net_5_posts AS p ON pm.meta_value=p.ID 
  WHERE pm.post_id = $ga_id
  AND pm.meta_key = '_thumbnail_id' 

  UNION ALL

  SELECT p.*
  FROM net_7_postmeta AS pm
  INNER JOIN net_7_posts AS p ON pm.meta_value=p.ID 
  WHERE pm.post_id = $ga_id
  AND pm.meta_key = '_thumbnail_id' 
) united
ORDER BY united.post_date DESC 
LIMIT 4
于 2012-04-16T17:54:42.180 回答