P
P
Pan Propan2014-05-14 16:01:31
PostgreSQL
Pan Propan, 2014-05-14 16:01:31

How to properly write condition for Postgresql view?

Hello, friends.
DBMS Postgresql 9.3.
Create view with multiple condition

CREATE OR REPLACE VIEW rastitelnos_line_10000 AS 
 SELECT *
   FROM rastitelnos_line
  WHERE (rastitelnos_line.objectkey !~ 'L0071131000') OR (rastitelnos_line.objectkey !~ 'L0071131100') OR (rastitelnos_line.objectkey !~ 'L0071224000') OR (rastitelnos_line.objectkey IS NULL);

ALTER TABLE rastitelnos_line_10000
  OWNER TO alimuradov;
GRANT ALL ON TABLE rastitelnos_line_10000 TO alimuradov;

If you add a record with the value specified in the filter to the source table
WHERE (rastitelnos_line.objectkey !~ 'L0071131000') OR (rastitelnos_line.objectkey !~ 'L0071131100') OR (rastitelnos_line.objectkey !~ 'L0071224000') OR (rastitelnos_line.objectkey IS NULL);

then this entry is not displayed in the view, if only one condition is left in the filter, for example
WHERE rastitelnos_line.objectkey !~ 'L0071224000' OR rastitelnos_line.objectkey IS NULL;

and make an entry with the value of this condition, then the entry in the view is displayed. I guess it's a syntax error.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Lesovsky, 2014-05-15
@lesovsky

yes, curious behavior...
try doing this

CREATE OR REPLACE VIEW rastitelnos_line_10000 AS 
 SELECT *
   FROM rastitelnos_line
  WHERE rastitelnos_line.objectkey NOT IN ('L0071131000','L0071131100','L0071224000') OR rastitelnos_line.objectkey IS NULL;

P
Pan Propan, 2014-05-15
@mgis

I created a condition with the query that you wrote, now no record is displayed in the view, moreover, when viewing the view from pgadmin, the condition filter has a strange look

WHERE (rastitelnos_line.objectkey::text <> ALL (ARRAY['L0071131000'::character varying, 'L0071131100'::character varying, 'L0071224000'::character varying]::text[])) OR rastitelnos_line.objectkey IS NULL;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question