1

我有2个表如下:

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

4 回答 4

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'
于 2012-10-29T05:57:00.327 回答
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 演示

于 2012-10-29T06:05:26.603 回答
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; 
于 2012-10-29T07:01:28.020 回答
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)
于 2012-10-29T05:54:56.293 回答