2015-08-08 82 views
1

我有两个表,commentuser。这里是我的结构:如何通过where子句限制连接子句

// 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

你可以用where做到这一点:

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

我知道了,只是我不知道语法,+1投票给你。 tnx – Shafizadeh

+0

我的问题是你的幸运问题,你现在有'10k'代表:-) – Shafizadeh

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 
+0

我知道,只是我不知道语法,+1 voteup你。 tnx – Shafizadeh