重命名内部联接并将其用于后续内部联接

时间:2015-10-06 16:01:16

标签: sql sqlite inner-join table-rename

所以我有三张桌子。

1) business:
-> id           (varchar)  id of business
-> name         (varchar)  name of business

2) review:
-> business_id  (varchar)  id of reviewed business
-> user_id      (varchar)  id of user who reviewed

3) user:
-> id           (varchar)  id of user
-> name         (varchar)  name of user
-> review_count (int)      number of reviews written by the user

我想编写一个查询,提取已经撰写超过10条评论的用户审核过的那些商家的business.idbusiness.name

我考虑在条件user reviewuser.id = review.user_iduser.id in (select id from user where review_count > 10)进行内部联接。在仅选择属于撰写超过10条评论的用户的user.id之后,这将在上表上执行内部联接。接下来我希望在这个新表和business表上执行内部联接,条件是business.id = new_table.business_id,其中new_table是通过执行上一次内部联接进行审核而获得的表用户。

我知道我的重命名语法可能存在问题。

我收到语法错误:Error: near "where": syntax error。我正在使用sqlite3。

到目前为止,这就是我所拥有的。

select business.id, business.name
from business inner join  
(                  
review inner join user
on review.user_id = user.id
where user.id in (
select id
from user
where review_count > 10) 
) as R
on R.business_id = business.id;

1 个答案:

答案 0 :(得分:1)

下面的查询应与您尝试的相同。

select business.id, business.name
from business                 
inner join review on review.business_id = business.id
inner join user on user.id = review.user_id 
where user.review_count > 10