我有两个表,comment
和user
。这是我的结构:
// 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
?
答案 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>