第一个问题是如此严厉:)
我需要按特定部门类型或部门排序,然后按等级排序。 默认设置按Rank,Dept_Type和Department排序。前端是PHP,但如果可能的话,我正在寻找更多的SQL修复程序。如果子查询可以工作,我也有一个部门的源表。
Orgin表:
+-----------+------------+------+
| Dept_Type | Department | Rank |
+-----------+------------+------+
| Inbound | Dept R | 1 |
| Outbound | Dept P | 2 |
| Inbound | Dept E | 3 |
| Outbound | Dept D | 4 |
| Outbound | Dept D | 5 |
| Outbound | Dept D | 6 |
+-----------+------------+------+
我需要通过“Dept D”,“Dept E”来指定订购,然后其他一切如此:
SELECT *
FROM table
ORDER BY FIELD(department, "Dept D", "Dept E"), Rank, Dept_Type, Department
我希望它返回(DDDERP):
+-----------+------------+------+
| Dept_Type | Department | Rank |
+-----------+------------+------+
| Outbound | Dept D | 4 |
| Outbound | Dept D | 5 |
| Outbound | Dept D | 6 |
| Inbound | Dept E | 3 |
| Inbound | Dept R | 1 |
| Outbound | Dept P | 2 |
+-----------+------------+------+
实际返回的内容(RPDDDE):
+-----------+------------+------+
| Dept_Type | Department | Rank |
+-----------+------------+------+
| Inbound | Dept R | 1 |
| Outbound | Dept P | 2 |
| Outbound | Dept D | 4 |
| Outbound | Dept D | 5 |
| Outbound | Dept D | 6 |
| Inbound | Dept E | 3 |
+-----------+------------+------+
所以理论上我想:
ORDER BY FIELD("Dept D", "Dept E", department), Rank
但显然这种方式不起作用。感谢您花时间看一下。
答案 0 :(得分:1)
使用DESC属性:
ORDER BY FIELD(department, 'Dept E', 'Dept D') DESC, Rank, Dept_Type, Department