Need help again about joining tables

前端 未结 1 712
傲寒
傲寒 2021-01-29 01:19

some context I asked a question about a MySQL request, my post can be found there: Need help about joining tables

Now I have another problem related to that, so on my pa

1条回答
  •  后悔当初
    2021-01-29 02:18

    Instead of filtering on an exact userid, you should check if the user is related to the issue, like this:

    select
      t.title,
      group_concat(
        case when tu.type = 1 then 
          concat(u.firstname, ' ', u.lastname)
        end) as creator,
      t.priority,
      t.date,
      group_concat(
        case when tu.type = 2 then 
          concat(u.firstname, ' ', u.lastname)
        end SEPARATOR ' - ') as users
    from
      tickets t
      inner join tickets_users tu on tu.ticketid = t.id
      inner join users u on u.id = tu.userid
    where 
      exists (
        select 
          'x' 
        from 
          tickets_users tu2 
        where 
          tu2.ticketid = t.id and 
          tu2.userid =  and 
          tu2.type = 1)
    group by
      t.id;
    

    For you can fill in the user id you want. This query will return all issues that are reported by that user (type = 1). But for all those issues, still all related users are returned, so your query result is still complete.

    0 讨论(0)
提交回复
热议问题