有关加入表格的再次需要帮助

时间:2013-07-19 22:24:40

标签: php mysql concat group-concat

一些上下文我问了一个关于MySQL请求的问题,我的帖子可以在那里找到:Need help about joining 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

的ID

除了创作者和用户之外,一切正常,因为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它将显示由用户创建的票号789 870用户1180和1632负责此票......

1 个答案:

答案 0 :(得分:1)

您应该检查用户是否与问题相关,而不是对确切的用户ID进行过滤,如下所示:

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;

对于<youruserid>,您可以填写所需的用户ID。此查询将返回该用户报告的所有问题(type = 1)。但是对于所有这些问题,仍会返回所有相关用户,因此您的查询结果仍然完整。