Jazztronik
01-07-2010, 05:12 PM
I need your help to build a query (in MySQL):
I've got a table called 'tickets' whose purpose is to store so-called 'trouble tickets' (or 'incident reports') created by customers. Those tickets may have assigned an order ID which the user previously made in the online shop, or not, because the ticket might represent some other type of feedback.
I need to get a list of all the tickets made by the current user whether they're assigned to an order ID or not. In case they are, the row must show the order number of the order. My first approach was this:
SELECT t. * , o.order_number AS order_number
FROM tickets t
INNER JOIN orders o ON t.order_id = o.order_id
WHERE t.author_id = $userid
ORDER BY t.creationdate DESC
This only listed the tickets made by the user which have been assigned an order ID. So, this was my second approach:
(
SELECT t. * , o.order_number AS order_number
FROM tickets t
INNER JOIN orders o ON t.order_id = o.order_id
WHERE t.author_id = $userid
ORDER BY t.creationdate DESC
)
UNION (
SELECT t. * , '-' AS order_number
FROM tickets t
WHERE t.author_id = $userid
ORDER BY t.creationdate DESC
)
However this is redundant. This query lists all the tickets made by the user, but the ones which have been assigned an order ID appear twice (one including the order number and another one showing a dash as the order number. I don't know how to show just one row per ticket (applying a GROUP BY clause or something else), and the 2nd approach seems to me to be anything but optimum.
Any help appreciated.
I've got a table called 'tickets' whose purpose is to store so-called 'trouble tickets' (or 'incident reports') created by customers. Those tickets may have assigned an order ID which the user previously made in the online shop, or not, because the ticket might represent some other type of feedback.
I need to get a list of all the tickets made by the current user whether they're assigned to an order ID or not. In case they are, the row must show the order number of the order. My first approach was this:
SELECT t. * , o.order_number AS order_number
FROM tickets t
INNER JOIN orders o ON t.order_id = o.order_id
WHERE t.author_id = $userid
ORDER BY t.creationdate DESC
This only listed the tickets made by the user which have been assigned an order ID. So, this was my second approach:
(
SELECT t. * , o.order_number AS order_number
FROM tickets t
INNER JOIN orders o ON t.order_id = o.order_id
WHERE t.author_id = $userid
ORDER BY t.creationdate DESC
)
UNION (
SELECT t. * , '-' AS order_number
FROM tickets t
WHERE t.author_id = $userid
ORDER BY t.creationdate DESC
)
However this is redundant. This query lists all the tickets made by the user, but the ones which have been assigned an order ID appear twice (one including the order number and another one showing a dash as the order number. I don't know how to show just one row per ticket (applying a GROUP BY clause or something else), and the 2nd approach seems to me to be anything but optimum.
Any help appreciated.