简体   繁体   中英

Is there a PostgreSQL equivalent of SQLite's IS operator?

In SQLite , IS is a binary operator that behaves exactly like = except when one or both of the operands are NULL . In the case where both operands are NULL , the IS operator evaluates to TRUE . In the case where one operand is NULL , but not the other, the IS operand evaluates to FALSE .

I was looking for a similar operator in PostgreSQL, but I could not find one. Is there an equivalent of SQLite's IS operator in PostgreSQL? If not, what is the best/least-complicated work-around?

To clarify, SELECT column1 IS column2 ... is allowed in SQLite, but PostgreSQL raises a syntax error.

尝试使用IS(NOT)DISTINCT FROM运算符。

Apparently it's possible in postgresql (see dan04's). The query below would work in SQL Server and other DBMSs where that syntax isn't available.

You can simulate by doing:

WHERE (column1 is NULL and column2 is NULL) 
      OR column1 = column2

To add to @Derek's answer:

In MySQL you can use the <=> operator to the same effect:

SELECT * FROM table1 WHERE column1 <=> column2

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