简体   繁体   中英

Postgresql 9.6. Check if a field is empty

In Postgresql 9.6, I have a table like this:

ID  ADDRESS_1  ADDRESS_2
 1    bac1       def2
 2    bac2  

If I execute this

SELECT exists (SELECT ADDRESS_2 FROM my_table WHERE ADDRESS_1='bac3');

I get False answer (expected)

However, if I execute this

SELECT exists (SELECT ADDRESS_2 FROM my_table WHERE ADDRESS_1='bac2');

I get True answer (Not expected)

I reckon it is because there is a Null value.

So my objective is to obtain False value for the last one.

Any ideas or thoughts?

Thank you in advance

The reason the second query is "failing" is because there is a record matching the condition in the EXISTS clause. If you add logic to also insist on a non NULL value in the ADDRESS_2 column, you should get the behavior you want:

SELECT EXISTS
    (SELECT ADDRESS_2 FROM my_table
     WHERE ADDRESS_1 = 'bac2' AND ADDRESS_2 IS NOT NULL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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