Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

sql - How to select the comparison of two columns as one column in Oracle

I cannot figure out how to add a column to my SELECT query indicating whether two columns contain the same data in Oracle.

I would like to write a query like:

select column1, column2, column1=column2 from table

and, if I have this table:

+---------+---------+
| column1 | column2 |
+---------+---------+
| value1  | value1  |
| value2  | value3  |
| value4  | value4  |
+---------+---------+

get a result like:

+---------+---------+-----------------+
| column1 | column2 | column1=column2 |
+---------+---------+-----------------+
| value1  | value1  | true            |
| value2  | value3  | false           |
| value4  | value4  | true            |
+---------+---------+-----------------+

What is the correct syntax to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to consider null values equality too, try the following

select column1, column2, 
   case
      when column1 is NULL and column2 is NULL then 'true'  
      when column1=column2 then 'true' 
      else 'false' 
   end 
from table;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...