简体   繁体   English

MySQL ORDER BY'ENUM'类型值

[英]MySQL ORDER BY 'ENUM' type value

I'm having a table: 我有一张桌子:

ID    TYPE     VALUE
1     phone    12345
2     fax      98753
3     address  etc
...

the TYPE col is defined as ENUM with values: 'phone','fax','address' . TYPE col被定义为ENUM ,其值包括: 'phone','fax','address'

Now my query is: 现在我的查询是:

$q = mysql_query(SELECT * FROM tbl ORDER BY id);
while(row = mysql_fetch_array($q)){
    echo $row['type'] . "--->" . $row['value'];
}

The output is ordered by id, first line is phone , then it's fax and then it's address . 输出按ID排序,第一行是phone ,然后是fax ,然后是address

But I want the output be order by: 但是我希望输出按以下顺序排序:

1- All address 2- All Phone 3- All fax 1-所有address 2-所有Phone 3-所有fax

How I could achieve this? 我该如何实现?

Thanks for your help :-) 谢谢你的帮助 :-)

SELECT A.*,  
case ID 
  when 3 then 1 
  when 2 then 3 
  when 1 then 2 
  else 4 end as myorder
FROM tbl A 
ORDER BY myOrder

Alternatively update the enum to list the Number first on the type field such as 01.Address, 02.Phone, 03.Fax .. then your order by type instead of ID. 或者,更新枚举以首先在类型字段(例如01.Address, 02.Phone, 03.Fax ..)上列出编号01.Address, 02.Phone, 03.Fax然后按类型而不是ID进行订购。 The 2nd approach makes it pretty easy to add up to 99 types without having to change code (just data) if you add another type later using the first approach, you have to edit the SQL... 第二种方法可以很容易地添加多达99种类型,而无需更改代码(仅数据),如果稍后使用第一种方法添加另一种类型,则必须编辑SQL ...

The first approach uses a case statement to generate a calculated column at run time. 第一种方法使用case语句在运行时生成计算列。 This new column contains the correct order by substituting the IDs for the order you want (it doesn't change the IDs, it simply defines them in the order you want) and then orders by this new calculated column. 该新列包含正确的订单,方法是将ID替换为所需的订单(它不会更改ID,它只是按照所需的顺序定义它们),然后按此新计算所得的列进行订购。

$q = mysql_query(SELECT A.*,  case ID when 3 then 1 when 2 then 3 when 1 then 2 else 4 end as myorder FROM tbl A ORDER BY myOrder);

The second approach involves changing the data in the enum such that it is preceeded by a number which defines the sort you want. 第二种方法涉及更改枚举中的数据,以使它前面带有定义所需排序的数字。 Thus if you change the SQL in your sort to sort by Type instead of ID, it would then work. 因此,如果将排序中的SQL更改为按类型而不是ID进行排序,则它将起作用。

$q = mysql_query(SELECT * FROM tbl ORDER BY Type);

Try this query - 试试这个查询-

SELECT * FROM tbl
ORDER BY
  CASE type
    WHEN 'address' THEN 1
    WHEN 'phone' THEN 2
    WHEN 'fax' THEN 3
    ELSE 4
  END

Full example: 完整示例:

CREATE TABLE tbl (
  id int(11) DEFAULT NULL,
  type enum ('phone', 'fax', 'address') DEFAULT NULL,
  value int(11) DEFAULT NULL
);

INSERT INTO test.tbl(id, type, value) VALUES
  (1, 'phone', 11),
  (2, 'fax', 22),
  (3, 'address', 33),
  (4, 'fax', 44),
  (5, 'address', 55);

SELECT * FROM tbl
ORDER BY
  CASE type
    WHEN 'address' THEN 1
    WHEN 'phone' THEN 2
    WHEN 'fax' THEN 3
    ELSE 4
  END;

+------+---------+-------+
| id   | type    | value |
+------+---------+-------+
|    3 | address |    33 |
|    5 | address |    55 |
|    1 | phone   |    11 |
|    2 | fax     |    22 |
|    4 | fax     |    44 |
+------+---------+-------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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