简体   繁体   English

SQL从多个表中选择并按特定条件排序

[英]SQL selecting from multiple tables and ordering by certain conditions

I have 2 tables, customer and tickets 我有2张桌子, customertickets

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.prioritytickets.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列,该列可以是ComputersTelephoneSoftware ,这就是需要链接到上面的内容。

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: 顾客:

  • Company A priority_computers = 1 公司A priority_computers = 1
  • Company B priority_computers = 2 公司B priority_computers = 2
  • Company C priority_computers = 3 公司C priority_computers = 3

Example One: 范例一:

  • Ticket 1 Company A priority = Medium category = Computers queue_time = 2015-11-20 08:00 票证1公司A优先级=中等类别=计算机队列时间= 2015-11-20 08:00
  • Ticket 2 Company B priority = Medium category = Computers queue_time = 2015-11-20 10:00:00 票证2公司B优先级=中等类别=电脑queue_time = 2015-11-20 10:00:00
  • Ticket 3 Company C priority = Medium category = Computers queue_time = 2015-11-20 08:30:00 票证3公司C优先级=中级类别=电脑queue_time = 2015-11-20 08:30:00

This should output in the following order: 这应该按以下顺序输出:

  • Ticket 3 票3
  • Ticket 2 票2
  • Ticket 1 票1

Example 2: 范例2:

  • Ticket 1 Company B priority = High category = Computers 票证1公司B优先级=高级类别=计算机
    queue_time = 2015-11-20 12:00 queue_time = 2015-11-20 12:00
  • Ticket 2 Company A priority = Medium category = Computers queue_time = 2015-11-20 07:00:00 票证2公司A优先级=中等类别=计算机队列时间= 2015-11-20 07:00:00
  • Ticket 3 Company C priority = Medium category = Computers queue_time = 2015-11-20 07:00:00 凭单3公司C优先级=中等类别=电脑queue_time = 2015-11-20 07:00:00

This should output in the following order: 这应该按以下顺序输出:

  • Ticket 1 票1
  • Ticket 3 票3
  • Ticket 2 票2

If I understand this correctly, then your problem is that you have to match data with column names somehow. 如果我正确理解这一点,那么您的问题就是您必须以某种方式将数据与列名进行匹配。

  • customer.priority_computers for ticket.category = 'Computers' ticket.category的customer.priority_computers ='计算机'
  • customer.priority_telephone for ticket.category = 'Telephone' ticket.category =“电话”的customer.priority_telephone
  • customer.priority_software for ticket.category = 'Software' ticket.category的customer.priority_software ='软件'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM