Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database PostgreSQL:如何使用自定义EmonsortOrder值定义枚举类型_Database_Postgresql_Enums - Fatal编程技术网

Database PostgreSQL:如何使用自定义EmonsortOrder值定义枚举类型

Database PostgreSQL:如何使用自定义EmonsortOrder值定义枚举类型,database,postgresql,enums,Database,Postgresql,Enums,是否有一种使用自定义顺序索引创建枚举类型的方法。之后可以更新pg_类型并将enumsortorder字段设置为所需的值,但问题是在创建过程中可以这样做吗?比如说 CREATE TYPE environment AS ENUM ('stage' 10, 'prod' 20); 排序顺序由类型声明定义: create type my_enum as enum ('first', 'third'); with t(v) as ( values ('first'::my_enum), ('t

是否有一种使用自定义顺序索引创建枚举类型的方法。之后可以更新pg_类型并将enumsortorder字段设置为所需的值,但问题是在创建过程中可以这样做吗?比如说

CREATE TYPE environment AS ENUM ('stage' 10, 'prod' 20);

排序顺序由类型声明定义:

create type my_enum as enum ('first', 'third');

with t(v) as (
    values ('first'::my_enum), ('third')
)
select *
from t
order by 1;

   v   
-------
 first
 third
(2 rows)
可以在所需位置添加新标签:

alter type my_enum add value 'second' before 'third';

with t(v) as (
    values ('first'::my_enum), ('third'), ('second')
)
select *
from t
order by 1;

   v    
--------
 first
 second
 third
(3 rows)    
了解和

如果上述内容不足,请使用映射功能,例如:

create or replace function my_enum_map(my_enum)
returns int language sql as $$
    select case $1 
        when 'first' then 100
        when 'second' then 50
        when 'third' then 75
    end
$$;

with t(v) as (
    values ('first'::my_enum), ('third'), ('second')
)
select *
from t
order by my_enum_map(v);

   v    
--------
 second
 third
 first
(3 rows)

您还可以将函数使用的值存储在表中,以便于修改。

排序顺序由类型声明定义:

create type my_enum as enum ('first', 'third');

with t(v) as (
    values ('first'::my_enum), ('third')
)
select *
from t
order by 1;

   v   
-------
 first
 third
(2 rows)
可以在所需位置添加新标签:

alter type my_enum add value 'second' before 'third';

with t(v) as (
    values ('first'::my_enum), ('third'), ('second')
)
select *
from t
order by 1;

   v    
--------
 first
 second
 third
(3 rows)    
了解和

如果上述内容不足,请使用映射功能,例如:

create or replace function my_enum_map(my_enum)
returns int language sql as $$
    select case $1 
        when 'first' then 100
        when 'second' then 50
        when 'third' then 75
    end
$$;

with t(v) as (
    values ('first'::my_enum), ('third'), ('second')
)
select *
from t
order by my_enum_map(v);

   v    
--------
 second
 third
 first
(3 rows)

您还可以将函数使用的值存储在表中,以便于修改。

请改为创建查找表。请改为创建查找表。谢谢您的回答。我的问题主要是关于自定义订单索引。然而,也许这是一个坏主意。我认为这个函数给了你你想要的,也许是另一种形式。但这不是一个坏主意,我在我的一个项目中成功地使用了类似的解决方案。谢谢你的回答。我的问题主要是关于自定义订单索引。然而,也许这是一个坏主意。我认为这个函数给了你你想要的,也许是另一种形式。但这不是一个坏主意,我在我的一个项目中成功地使用了类似的解决方案。