投诉数据库设计SQL查询以显示所有行



我有一个应用程序,我在其中向已登录的代理显示所有投诉。我有100名代理人可以看到相同的投诉屏幕。例如代理人A和代理人B当他们登录时可以看到所有的投诉。

> Complaint_id  Complaint_detail
        1            complaint_1    
        2            complaint_2    
        3            complaint_3    

现在的问题是,我必须添加每个代理都可以轻松发表评论的功能,或者你可以说这样的提醒(代理a发表评论:我明天会处理这个评论)。因此该注释将仅显示给代理A。

对于这个实现,我创建了一个名为complaint_detail的新表,在其中添加了"comment"one_answers"user_id"

并显示投诉我写查询

    select complaint.Complaint_name,complaint.User_ID from complaint 
left outer join complaint_detail on complaint.Complaint_id = complaint_detail.complaint_id

这个查询现在显示所有记录,当我过滤用户时,它将只显示用户记录来解决这个问题。我添加

    select * from (select complaint.Complaint_name,complaint.User_ID from complaint 
left outer join complaint_detail on complaint.Complaint_id = complaint_detail.complaint_id
complaint_detail.complaint_info_id
) asdf

其中user_id="agentA"或者User_ID为空

select * from (
select complaints.complaint_id,complaints.complaint_detail,   complaints_detail.comment,complaints_detail.user_id from complaints
left outer join complaints_detail on complaints.Complaint_id = complaints_detail.complaint_id
) asdf
where user_id = 'agentA'
or User_ID is null

complaint_id    complaint_detail    comment        user_id
         1             complaint_1         complaint_1  agentA
         2             complaint_2         complaint_2  agentA
         3             complaint_3           null            null

对于代理B

complaint_id    complaint_detail    comment        user_id
     1             complaint_1          complaint1_ agentB
     3             complaint_3           null            null

我知道我该如何做到每个用户都能看到所有投诉,只有他们的评论。我应该改变表结构还是查询才能做到这一点?

应该这样做:

select * from complaints cmp
left outer join comments com on cmp.id=com.complaint_id
and com.user_id='agentA' or com.user_id is null 

这将从与投诉相关的评论表中获取数据(如果存在)(左联接)并将评论限制为代理的评论或评论上没有用户id的评论

当然,如果您不想从投诉&注释表。

最新更新