修订。我的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
答案 0 :(得分:1)
正如你所说父表没有任何关系我只能假设你想要将 5 和 7 表视为相同。您应该使用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