我应该更改我的sql语句,以便我可以使用此表到达?

时间:2017-03-05 16:38:28

标签: mysql sql

您好我遇到了这个问题我有桌子

投诉:

Complaint table

用户:

User table

我想提取对特定省份用户的投诉

示例:如果我想提取ILOCOS省的投诉,我的表格看起来像这样

complaint_id | user_id | complaint_title| complaint_category | complaint_desc
---------------------------------------------------------------------------------
17           | 2       | 2              | Accidents          | qwe123

我已经带着这个sql语句到达了:

SELECT DISTINCT complaint.complaint_id, complaint.user_id,
      complaint.complaint_title,complaint.complaint_desc                         
FROM complaint LEFT JOIN
     user
     ON user.province = 'ILOCOS' LEFT JOIN
     complaint_media
     ON complaint.complaint_id = complaint_media.complaint_id

1 个答案:

答案 0 :(得分:0)

您需要在user_id列上加入投诉表和用户表,否则它将是一个笛卡尔联接。

select distinct complaint.complaint_id,   -- check if you really need "DISTINCT"
    complaint.user_id,
    complaint.complaint_title,
    complaint.complaint_desc,
    . . .
from complaint
join user
    on complaint.user_id = user.user_id     -- here
        and user.province = 'ILOCOS'
join complaint_media
    on complaint.complaint_id = complaint_media.complaint_id