I have 2 tables, customer
and tickets
I want to be able to select from the tickets table and order by:
tickets.priority
then customer.category_priority
then tickets.queue_time
I have this query:
SELECT t.*
from tickets t
JOIN customer c ON t.company = c.sequence
WHERE t.status <> 'Completed' AND t.queue = 'Y'
ORDER BY field(t.priority, 'Critical', 'High', 'Medium', 'Low'), t.queue_time ASC
which works great for the tickets.priority
and tickets.queue_time
but im not sure how to incorporate the customer.category_priority
so in the customer table, i have columns with names like:
priority_computers
priority_telephone
priority_software
all INT fields and have a value of 0, 1 or 2
the row in tickets
has a category
column which is either Computers
, Telephone
, or Software
and thats what needs to link to the above.
so, if a customer row has priority_computers
of 2 and the tickets
row is category = 'Computers'
that would be at the top of the list because the customer record has the priority of 2
and it would also incorporate the other ORDER BY
conditions
Examples:
Customers:
Example One:
This should output in the following order:
Example 2:
This should output in the following order:
If I understand this correctly, then your problem is that you have to match data with column names somehow.
This shows a database design flaw. There should be a customer_priority table instead with each row holding a customer, a category and the associated value. Then you could simply join.
As is, you must check data content and decide for a column to use in your query:
SELECT t.*
from tickets t
JOIN customer c ON t.company = c.sequence
WHERE t.status <> 'Completed' AND t.queue = 'Y'
ORDER BY field(t.priority, 'Critical', 'High', 'Medium', 'Low')
, case t.category
when 'Computers' then c.priority_computers
when 'Telephone' then c.priority_telephone
when 'Software' then c.priority_software
end
, t.queue_time ASC
Update: Here is a query you'd write, if you had a customer_priority table. You see, your query doesn't need to know what categories exist and how to treat them any longer.
SELECT t.*
from tickets t
JOIN customer c ON t.company = c.sequence
JOIN customer_priority cp ON cp.customer_id = c.sequence
AND cp.category = t.category
WHERE t.status <> 'Completed' AND t.queue = 'Y'
ORDER BY field(t.priority, 'Critical', 'High', 'Medium', 'Low')
, cp.priority_value
, t.queue_time ASC
Moreover: As mentioned, it is strange to have a table customer
, but in tickets
it's not called a customer , but a company , and in the customer
table itself it's not called a customer number or ID either, but a sequence . This makes the queries less readable. I suggest, you change the names, if possible, so they are consistent.
Also your query shouldn't have to know what 'Critical' and 'High' means. There should be a table for priorities where each priority has a name and a value, so the query could simply pick the value and work with it without having to know anything else about the priorities.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.