问题
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 JOIN
ing 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