SQL double comparison

一笑奈何 提交于 2020-01-15 07:15:53

问题


let's say I have following SQL column:

Now, I want to do following: In col_1, I have the value 'one'. If I check, which corresponding values I get in col_2 I get for that, I find 'two' and 'three'. So the results would be 'two' and 'three'. But from this result set, I want to only have those, that, used in col_1, have, in col_2, the corresponding value 'one'. So: 'two', in col_1, does have 'one' in col_2, but three doesn't. So, from the result set of {'two', 'three'} only {'two'} would remain.

How can I make such a double-checking query with MySQL?

Thanks in advance!


回答1:


This is done by JOINing a table to itself with a given condition. Assuming your table name is table:

SELECT t1.col_1
FROM table t1 JOIN table t2 ON t1.col_2=t2.col_1
WHERE t1.col_1=t2.col_2 
AND t1.col_1='one';

This gives:

 col_1 
-------
 one
(1 row)


来源:https://stackoverflow.com/questions/5611125/sql-double-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!