如何通过where子句限制join子句

时间:2015-08-08 13:04:05

标签: mysql join where

我有两个表,commentuser。这是我的结构:

// comment
+----+---------+---------+---------+
| id | id_user | id_post | content |
+----+---------+---------+---------+

// user
+----+------+
| id | name |
+----+------+

我想从user表(而不是id_user)访问用户名。这是我的疑问:

select c.content, u.name from comment c inner join user u on c.id_user=u.id;

它给了我这个结构:

+---------+------+
| content | name |
+---------+------+

结构很好,但我只需要选择属于帖子x的评论。换句话说,如何在查询中使用id_post

2 个答案:

答案 0 :(得分:2)

如何使用WHERE子句:

SELECT c.content, u.name
FROM comment c INNER JOIN user u
ON c.id_user=u.id
WHERE c.id_post = x

答案 1 :(得分:2)

您可以使用where执行此操作:

select c.content, u.name
  from comment c inner join user u on c.id_user = u.id
  where c.id_post = <x>