我的整个查询有点复杂,所以我会尝试展示你需要了解我的问题,我从数据库中获取门票,它显示标题,创建者,负责人票证和最后更新者,对于创作者和负责人,我在ID
中找到他们的tickets_users.users_id
和他们的类型1或2,这取决于他们是票的创建者还是他们负责,对于故障单的标题和所有信息,表格现在被命名为tickets
,用户的ID就是他们的名字等等users
所以我的查询看起来像这样:
SELECT
tickets.id,
tickets.name,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 1 THEN
CONCAT(users.firstname, ' ', users.realname)
END) AS creator,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 1 THEN
CONCAT(tickets_users.users_id)
END) AS creator_id,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 2 THEN
CONCAT(users.firstname, ' ', users.realname)
END) AS in_charge,
GROUP_CONCAT(
CASE WHEN tickets_users.type = 2 THEN
CONCAT(tickets_users.users_id)
END) AS in_charge_id,
tickets.date,
tickets.priority,
tickets.date_mod,
tickets.status,
tickets.users_id_lastupdater,
MAX(CASE WHEN tickets.users_id_lastupdater = users.id
THEN CONCAT(users.firstname, ' ', users.realname)
END) AS last_updater,
tickets.content
FROM
tickets
JOIN tickets_users ON tickets_users.tickets_id = tickets.id
JOIN users ON users.id = tickets_users.users_id
WHERE tickets.id = ?");
我的问题是JOIN users ON users.id = tickets_users.users_id
它适用于负责人和创作者,但如果有人不负责回复或修改故障单,他将不会出现在“最后修改过”中,因为{{1 } last_updater
位于表ID
中。
我试图加入tickets
,但它没有用。有什么想法吗?
答案 0 :(得分:1)
ON users.id IN (tickets_users.users_id, tickets.last_updater)
并且
您需要将测试
AND users.id = tickets_users.users_id
添加到 你的CASE
陈述
工作,谢谢eggyal!