2013-02-10 59 views
1

我有2个表 - 每个表都有一个相同的字段 - 我们将称之为ID。我需要从表1和表2中提取所有数据,其中id =变量,如$变量。我已经看到了答案,我可以从两个表中的id字段相同的数据中提取数据,但这不是我正在寻找的。这是我当前的代码:两个表1变量

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' 

任何有关如何正确编码的帮助将不胜感激。

+0

什么是这两个表之间的关系? – 2013-02-10 06:47:14

+0

你面对这个查询会遇到什么问题?我看起来很好。 – hjpotter92 2013-02-10 06:49:06

回答

0

要想从哪里该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' 
+0

感谢您的快速响应 - 我现在唯一的问题是,我没有显示结果:$ groupid ='2'; $ clients = mysql_query(“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 ='$ groupuri'”)or die(mysql_error() ); //我们打算为这个查询使用“$ row”方法。这只是我的偏好。 while($ row = mysql_fetch_array($ clients)){ $ topic_uri = $ row [“topic_uri”]; echo $ topic_uri; $ row_count ++; \t } – 2013-02-10 07:03:09

+0

没关系 - 我被推迟了。我在我的sql脚本中打错了我的变量。非常感谢您的帮助 – 2013-02-10 07:07:57

+0

@CalvinFowler - 它确定,随时欢迎您:) – 2013-02-10 09:16:51