Mysql 我应该如何更改我的sql语句,以便可以使用此表?

Mysql 我应该如何更改我的sql语句,以便可以使用此表?,mysql,sql,Mysql,Sql,你好,我被这个问题困扰了我有桌子 投诉: 用户: 我想提取特定省份用户的投诉 示例:如果我想提取ILOCOS省的投诉,我的表将如下所示 complaint_id | user_id | complaint_title| complaint_category | complaint_desc --------------------------------------------------------------------------------- 17 | 2

你好,我被这个问题困扰了我有桌子

投诉:

用户:

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

示例:如果我想提取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

您需要将投诉表与user_id列上的user表联接,否则它将是笛卡尔联接

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

哦,它不需要区分,也没有得到我想要的,它仍然显示所有的投诉:(不是省政府的投诉ILOCOS@JoshuaLee-那是因为你使用的是左连接。那么你想要一个内部连接。更新了答案。@JoshuaLee-你不需要为
内部连接
编写
内部连接
。只要
连接
就够了。默认情况下,它只是内部连接。非常感谢你,现在它工作正常了,我真的不太好随着加入表格phew:D你是一个救生员,我现在不会诉诸于我心目中的暴力手段哈哈哈,我不知道哈哈哈