简体   繁体   中英

How to compare values of text columns from the same table

I have a table in which each column represent a text from a manuscript, here is a simple example:

mss1  | mss2  | mss3
------------------------
The   |  The  |  A
big   |  big  |  big
black |       |
dog   |  dog  |  dog

I would like to display rows where two columns have different values (or the same values), for instance I want to see the differences between mss1 and mss3. The result should look like:

mss1  | mss3
---------------
The   |  A
black |

These seemed to be good solution candidates :

SELECT mss1, mss3 FROM table WHERE mss1 != mss3;

SELECT mss1, mss3 FROM table WHERE mss1 NOT LIKE mss3;

However it is not working, even after converting all columns from text to varchar of the same length.

I also tried LOCATE ( See here ) to find same values: if I can locate mss1 in mss3 and vice-versa, they must have the same value, right? But that was not successful either. Any idea? It seems like it should be easy, but I can't figure it out...

Perhaps the issue with your query is that NULL values are filtered out. For your purposes, I think this may do what you want:

SELECT mss1, mss3
FROM table
WHERE coalesce(mss1, '') <> coalesce(mss3, '');

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