我有 2 个表 - 每个表都有一个相同的字段 - 我们称之为 id。我需要从表 1 和表 2 中提取所有数据,其中 id = 一个变量,例如 $variable。我已经看到答案,其中我可以从两个表中的 id 字段相同的两个表中提取数据,但这不是我想要的。这是我当前的代码:
SELECT
topic_title,
topic_uri
FROM bx_groups_forum_topic
RIGHT JOIN bx_groups_forum_post
ON (bx_groups_forum_topic.topic_id = bx_groups_forum_post.topic_id)
WHERE bx_groups_forum_topic.forum_id = '$variable'
如能提供有关如何正确编码的任何帮助,我们将不胜感激。
最佳答案
要仅从 id topic_id
在两者中相同的表中获取数据,请尝试使用 INNER JOIN
而不是 RIGHT JOIN
:
SELECT
t.topic_title,
t.topic_uri
FROM bx_groups_forum_topic AS t
INNER JOIN bx_groups_forum_post AS p ON t.topic_id = p.topic_id
WHERE t.forum_id = '$variable'
关于php - 两张表1个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14795409/