Postgres选择枚举数组中特定数组的位置

时间:2018-03-06 13:08:56

标签: sql postgresql

给出这个例子:

表:

CREATE TABLE public.animals
(
  name character varying(64),
  whitelist animal_whitelist[]
)

自定义枚举类型animal_whitelist:

CREATE TYPE public.animal_whitelist AS ENUM
  ('dog',
  'cat',
  'bird');

如何专门选择白名单。

在伪代码中,这是我想要选择的内容。

  • animal_whitelist等于dog
  • 的所有行
  • animal_whitelist等于dog cat
  • 的所有行
  • animal_whitelist等于dog cat
  • 的所有行
  • animal_whitelist dogcatbird
  • 的所有行

2 个答案:

答案 0 :(得分:1)

请检查one

    insert into animals ("name", "whitelist") values ('bobic', array['dog']::animal_whitelist[]);
insert into animals ("name", "whitelist") values ('barsic', array['cat']::animal_whitelist[]);
insert into animals ("name", "whitelist") values ('pet', array['dog', 'cat', 'bird']::animal_whitelist[]);
insert into animals ("name") values ('jim');


-- Any rows where the animal_whitelist equals dog
select * from animals where  array['dog']::animal_whitelist[] = "whitelist" ;

-- Any rows where the animal_whitelist equals dog and cat
select * from animals where  array['dog', 'cat']::animal_whitelist[] = "whitelist" ;

-- Any rows where the animal_whitelist equals dog or cat
select * from animals where  array['dog', 'cat']::animal_whitelist[] <@ "whitelist" ;

-- Any rows where the animal_whitelist is not dog, cat, or bird
select * from animals where not array['dog', 'cat', 'bird']::animal_whitelist[] && "whitelist";

答案 1 :(得分:0)

我想我通过一些更多的实验来解决这个问题

  1. Traceback (most recent call last): File "<ipython-input-1-00512d6cb966>", line 1, in <module> runfile('Private, so deleted that one here') File "/usr/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile execfile(filename, namespace) File "/usr/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace) File "/home/ttp/manbrunn/Documents/NNLOJETexe/HJtest/funktioniert.py", line 15, in <module> plt.plot(df['yh_center[2]'], df['tot_scale01[4]'], 'ro') File "/usr/lib64/python3.4/site-packages/matplotlib/pyplot.py", line 3099, in plot ret = ax.plot(*args, **kwargs) File "/usr/lib64/python3.4/site-packages/matplotlib/axes/_axes.py", line 1374, in plot self.add_line(line) File "/usr/lib64/python3.4/site-packages/matplotlib/axes/_base.py", line 1504, in add_line self._update_line_limits(line) File "/usr/lib64/python3.4/site-packages/matplotlib/axes/_base.py", line 1515, in _update_line_limits path = line.get_path() File "/usr/lib64/python3.4/site-packages/matplotlib/lines.py", line 874, in get_path self.recache() File "/usr/lib64/python3.4/site-packages/matplotlib/lines.py", line 575, in recache x = np.asarray(xconv, np.float_) File "/usr/lib64/python3.4/site-packages/numpy/core/numeric.py", line 462, in asarray return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'upper'

  2. SELECT * FROM animals WHERE whitelist @> ARRAY['dog'::whitelist]

  3. SELECT * FROM animals WHERE whitelist @> ARRAY['dog'::whitelist, 'cat'::whitelist] AND NOT whitelist @> ARRAY['bird'::whitelist]

  4. SELECT * FROM animals WHERE whitelist @> ARRAY['dog'::whitelist] OR whitelist @> ARRAY['cat'::whitelist]