首页 文章资讯内容详情

MySQL ORDER BYENUM类型值基于条件

2026-06-04 1 花语

为此,请使用ORDERBYCASE语句。让我们首先创建一个表,其中有ENUM类型的列-

create table DemoTable1461 -> ( -> DeckOfCards ENUM(K,J,A,Q) -> );

使用插入命令在表中插入一些记录-

insert into DemoTable1461 values(K); insert into DemoTable1461 values(A); insert into DemoTable1461 values(J); insert into DemoTable1461 values(Q);

使用select语句显示表中的所有记录-

select * from DemoTable1461;

这将产生以下输出-

+-------------+ | DeckOfCards | +-------------+ | K | | A | | J | | Q | +-------------+ 4 rows in set (0.00 sec)

以下是按ENUM类型值排序的查询-

select * from DemoTable1461 -> order by -> case DeckOfCards when A then 100 -> when K then 101 -> when Q then 102 -> else 103 -> end;

这将产生以下输出-

+-------------+ | DeckOfCards | +-------------+ | A | | K | | Q | | J | +-------------+ 4 rows in set (0.00 sec)