Mysql内连接子句不起作用

时间:2012-10-29 05:50:06

标签: mysql sql

我有两张表格如下:

talk表格

topic_id | topic_name | user_id (user who create topic)
  1           test1        1
  2           test2        2

talk_reply表格

talk_reply_id | message | topic_id | user_id (user who reply to specific topic)
     1            hi1        1         3
     2            hi2        1         4

user表格

user_id  | name
  1        mark
  2        pedro
  3        gates
  4        steve

我的查询如下:

SELECT `tr`.`message`, 
       `tr`.`user_id`, 
       `tr`.`topic_id`,  
       `u`.`name` 
FROM `talk_reply` AS `tr`
INNER JOIN `users` AS `u` ON tr.user_id = u.user_id WHERE (tr.topic_id=1)

但我只得到1结果而不是2结果,我在这里缺少什么?感谢。

4 个答案:

答案 0 :(得分:1)

SELECT t.topic_id, t.user_id, u.name, tr.message
FROM talk t
INNER JOIN talk_reply tr ON tr.topic_id = t.topic_id
INNER JOIN users u ON u.user_id = t.user_id
WHERE t.topic_id = '1'

<强>更新

SELECT tr.topic_id, tr.user_id, u.name, tr.message
FROM talk_reply tr
INNER JOIN users u ON u.user_id = tr.user_id
WHERE tr.topic_id = '1'

答案 1 :(得分:1)

SELECT tr.`message`, 
       tr.`user_id`, 
       tr.`topic_id`,  
       (select u.`name` from users u where u.user_id = tr.user_id) name
FROM `talk_reply` as tr
WHERE tr.topic_id=1

SQLFIDDLE DEMO

答案 2 :(得分:1)

select a.topic_id, a.user_id,b.message,c.name 
from talk a 
inner join talk_reply.b on a.topic_id=b.topic.id
inner join users c on c.user_id=a.user_id 
where t.topic_id=1; 

答案 3 :(得分:0)

使用此:

SELECT `tr`.`message`, 
       `tr`.`user_id`, 
       `tr`.`topic_id`,  
       `u`.`name` 
FROM `talk_reply` AS `tr`
AND `users` AS `u` 
WHERE tr.user_id = u.user_id AND (tr.topic_id=1)