SQL双重比较 [英] SQL double comparison
问题描述
假设我有以下SQL列:
let's say I have following SQL column:
现在,我要执行以下操作:
在col_1中,我的值为'1'。如果我检查,我在col_2中得到哪些相应的值,我得到了,我发现'二'和'三'。所以结果将是两和三。但是,从这个结果集,我想只有在 col_1
中使用的那些,在col_2中有相应的值'one'。所以:
'two',在 col_1
, 在 col_2
,但三个不。因此,从{'two','three'}只有{'two'}的结果集将保留。
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.
我如何进行这样的双重查询与MySQL有关?
How can I make such a double-checking query with MySQL?
提前感谢
推荐答案
通过 JOIN
将一个表以给定的条件置于自己身上。假设你的表名是表
:
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';
这给出:
col_1
-------
one
(1 row)
这篇关于SQL双重比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!