[英]SQL selecting from multiple tables and ordering by certain conditions
I have 2 tables, customer
and tickets
我有2张桌子,
customer
和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
tickets.priority
然后customer.category_priority
然后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
这对
tickets.priority
和tickets.queue_time
非常tickets.queue_time
but im not sure how to incorporate the customer.category_priority
但我不确定如何合并
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 所有INT字段,其值为0、1或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. tickets
的行具有category
列,该列可以是Computers
, Telephone
或Software
,这就是需要链接到上面的内容。
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 因此,如果客户行的
priority_computers
为2,而tickets
行的category = 'Computers'
,则该行将在列表的顶部,因为客户记录的优先级为2
,并且还包含其他ORDER BY
条件
Examples: 例子:
Customers: 顾客:
Example One: 范例一:
This should output in the following order: 这应该按以下顺序输出:
Example 2: 范例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.
应该有一个customer_priority表,而不是每一行都包含一个客户,一个类别和关联的值。 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. 更新:如果您有一个customer_priority表,这是您要编写的查询。 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 . 而且:如前所述,有一个表
customer
是很奇怪的,但是在tickets
它不是客户 ,而是公司 ,在customer
表本身中,它也不是客户编号或ID,而是序列 。 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.
应该有一个优先级表,其中每个优先级都有一个名称和一个值,因此查询可以简单地选择值并使用它,而不必了解其他有关优先级的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.