0

一些上下文我问了一个关于 MySQL 请求的问题,我的帖子可以在那里找到:Need help about join tables

现在我有另一个与此相关的问题,所以在我的页面上,我列出了来自数据库的票,我的请求是:

"SELECT
    glpi_tickets.id,
    glpi_tickets.name,
    GROUP_CONCAT(
            CASE WHEN glpi_tickets_users.type = 1 THEN 
            CONCAT(glpi_users.firstname, ' ', glpi_users.realname)
        END) AS creator,
    GROUP_CONCAT(
        CASE WHEN glpi_tickets_users.type = 1 THEN 
            CONCAT(glpi_tickets_users.users_id)
        END) AS creator_id,
    GROUP_CONCAT(
        CASE WHEN glpi_tickets_users.type = 2 THEN 
            CONCAT(glpi_users.firstname, ' ', glpi_users.realname)
        END) AS users,
    GROUP_CONCAT(
        CASE WHEN glpi_tickets_users.type = 2 THEN 
            CONCAT(glpi_tickets_users.users_id)
        END) AS users_id,
    glpi_tickets.date,
    glpi_tickets.priority,
    glpi_tickets.date_mod,
    glpi_itilcategories.completename,
    glpi_tickets.status,
    glpi_tickets.users_id_lastupdater,
    GROUP_CONCAT(
        CASE WHEN glpi_tickets.users_id_lastupdater = glpi_users.id THEN
            CONCAT(glpi_users.firstname, ' ', glpi_users.realname) 
        END SEPARATOR '<br>') AS last_updater,
    glpi_tickets.content
    FROM
        glpi_tickets
    JOIN glpi_tickets_users ON glpi_tickets_users.tickets_id = glpi_tickets.id
    JOIN glpi_users ON glpi_users.id = glpi_tickets_users.users_id
    JOIN glpi_itilcategories ON glpi_itilcategories.id = glpi_tickets.itilcategories_id
    GROUP BY
        glpi_tickets.id"

结果如下所示:

[ID][Title][creator][date created][priority][category][status][date modified][assigned to][last update by]
[125][helpdesk test][admin][29-01-2013 21:09][low][messaging][new][30-01-2013 17:52][Tony][admin]

对于每张工单,creator - creator_id 和 users - users_id 可以包含多个 ID 和名称。

现在我想在另一个页面中显示用户信息,我使用了相同的请求,WHERE glpi_users.id = ?但从users.php?id=x

除了创建者和用户之外,一切都很好,因为WHERE glpi_users.id = x只有一个人选择。

我想要一种显示方式,就像之前的请求一样,在此先感谢

编辑:尝试了 GolezTrols 解决方案,但它不起作用,这是我的查询:

"SELECT
                        glpi_tickets.id,
                        glpi_tickets.name,
                        GROUP_CONCAT(
                            CASE WHEN glpi_tickets_users.type = 1 THEN 
                                CONCAT(glpi_users.firstname, ' ', glpi_users.realname)
                            END) AS creator,
                        GROUP_CONCAT(
                            CASE WHEN glpi_tickets_users.type = 1 THEN 
                                CONCAT(glpi_tickets_users.users_id)
                            END) AS creator_id,
                        GROUP_CONCAT(
                            CASE WHEN glpi_tickets_users.type = 2 THEN 
                                CONCAT(glpi_users.firstname, ' ', glpi_users.realname)
                            END) AS users,
                        GROUP_CONCAT(
                            CASE WHEN glpi_tickets_users.type = 2 THEN 
                                CONCAT(glpi_tickets_users.users_id)
                            END) AS users_id,
                        glpi_tickets.date,
                        glpi_tickets.priority,
                        glpi_tickets.date_mod,
                        glpi_itilcategories.completename,
                        glpi_tickets.status,
                        glpi_tickets.users_id_lastupdater,
                        GROUP_CONCAT(
                            CASE WHEN glpi_tickets.users_id_lastupdater = glpi_users.id THEN
                                CONCAT(glpi_users.firstname, ' ', glpi_users.realname) 
                            END SEPARATOR '<br>') AS last_updater,
                        glpi_tickets.content
                    FROM
                        glpi_tickets
                        JOIN glpi_tickets_users ON glpi_tickets_users.tickets_id = glpi_tickets.id
                        JOIN glpi_users ON glpi_users.id = glpi_tickets_users.users_id
                        JOIN glpi_itilcategories ON glpi_itilcategories.id = glpi_tickets.itilcategories_id
                    WHERE
                       exists (
                            SELECT 
                              'x' 
                            FROM
                              glpi_tickets_users
                            WHERE
                            glpi_tickets_users.tickets_id = glpi_tickets.id AND
                            glpi_tickets_users.id = ? AND
                            glpi_tickets_users.type = 1)
                    GROUP BY
                        glpi_tickets.id"

几乎所有时间它根本没有显示票,有时它显示票但与用户无关,例如,如果我尝试users.php?id=1536它会显示由用户 870 创建的票号 789,用户 1180 和 1632 负责这张票...

4

1 回答 1

1

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 = <youruserid> and 
      tu2.type = 1)
group by
  t.id;

For <youruserid> 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.

于 2013-07-20T23:29:10.780 回答