【发布时间】:2015-11-20 19:23:55
【问题描述】:
我有 2 张桌子,customer 和 tickets
我希望能够从门票表中选择并通过以下方式订购:
tickets.priority 然后customer.category_priority 然后tickets.queue_time
我有这个问题:
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
这对tickets.priority 和tickets.queue_time 非常有用
但我不确定如何合并customer.category_priority
所以在客户表中,我的列的名称如下:
priority_computers
priority_telephone
priority_software
所有 INT 字段,值为 0、1 或 2
tickets 中的行有一个category 列,可以是Computers、Telephone 或Software,这就是需要链接到上面的内容。
因此,如果客户行的 priority_computers 为 2,而 tickets 行是 category = 'Computers',则该行将位于列表顶部,因为客户记录的优先级为 2,它还将包含其他ORDER BY条件
例子:
客户:
- A 公司 priority_computers = 1
- 公司 B priority_computers = 2
- C 公司 priority_computers = 3
示例一:
- Ticket 1 公司 A 优先级 = 中等类别 = 计算机 queue_time = 2015-11-20 08:00
- Ticket 2 公司 B 优先级 = 中等类别 = 计算机 queue_time = 2015-11-20 10:00:00
- Ticket 3 公司 C 优先级 = 中等类别 = 计算机 queue_time = 2015-11-20 08:30:00
这应该按以下顺序输出:
- 票 3
- 票2
- 票 1
示例 2:
- 工单 1 公司 B 优先级 = 高类别 = 计算机
queue_time = 2015-11-20 12:00 - Ticket 2 公司 A 优先级 = 中等类别 = 计算机 queue_time = 2015-11-20 07:00:00
- Ticket 3 公司 C 优先级 = 中等类别 = 计算机 queue_time = 2015-11-20 07:00:00
这应该按以下顺序输出:
- 票 1
- 票 3
- 票2
【问题讨论】:
-
你能给出示例数据和预期输出吗?
-
检查我的更新,我给出了一些虚拟数据的例子,希望对您有所帮助
-
所以每张票只有一位顾客? (
JOIN customer c ON t.company = c.sequence有点令人困惑,我原以为是JOIN customer c ON t.customer_id = c.customer_id)。还是一张票可以有更多的顾客?如果有,如何处理? -
每张票有一位顾客,但每位顾客可以拥有超过 1 张票
-
如果有更好的方法将此信息存储在数据库中,那么我愿意接受意见