问题
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 page I'm listing tickets from a database my request is:
"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"
The result look like:
[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]
For each ticket, creator - creator_id and users - users_id can contain multiple ID and name.
Now I want to display user information in another page, I used the same request but with WHERE glpi_users.id = ?
getting the id from users.php?id=x
Everything works fine except the the creator and users, since the WHERE glpi_users.id = x
only select one person.
I'd like a way to display then like in the previous request, thanks in advance
EDIT: Tried the GolezTrols solution but it's not working, here is my query:
"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"
Almost all the time it's showing no tickets at all, and sometimes it's showing a ticket but not related to the user, for example if I try users.php?id=1536
it will show the ticket number 789 that is created by the user 870 with user 1180 and 1632 in charge of this ticket...
回答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.
来源:https://stackoverflow.com/questions/17756439/need-help-again-about-joining-tables