[英]How to select MySQL rows that have the same content but in different columns?
I have a MySQL table with three columns, X, Y and Z 我有一个包含三列X,Y和Z的MySQL表
| X | Y | Z |
-------------------------------------
| john | patrick | active |
| john | miles | inactive |
| patrick | john | active |
I'd like to select the row data, given 'john' as the input, corresponding to the pair john-patrick - that is, john and patrick are both in either X or Y (like select * rows from TABLE where X = john or Y = john and where ...) 我想选择行数据,以“ john”作为输入,对应于一对john-patrick-也就是说,john和patrick都在X或Y中(例如从TABLE中选择*行,其中X = john或Y = john,其中...)
Is it possible? 可能吗? If so, how? 如果是这样,怎么办?
If I understand you correctly, the values need to be in X and Y (excluding the tuple ('john', 'miles', 'inactive') in the sample). 如果我理解正确,则值必须为X 和 Y(样本中不包括元组(“ john”,“ miles”,“ inactive”))。
Then you will need subquerys for this: 然后,您将需要以下子查询:
select X,Y,Z from TABLE where
(X = 'john' or Y = 'john') and
X in (select Y from TABLE) and
Y in (select X from TABLE);
EDIT: The first query was wrong, thanks to ypercube (see comments). 编辑:第一个查询是错误的,感谢ypercube(请参阅注释)。
Corrected version: 更正的版本:
select X,Y,Z from TABLE where
(X = 'john' or Y = 'john') and
X in (select Y from TABLE as subTable WHERE
TABLE.X = subTable.Y and TABLE.Y = subTable.X) and
Y in (select X from TABLE as subTable WHERE
TABLE.X = subTable.Y and TABLE.Y = subTable.X);
select a, b, c from test where b in (select distinct a from test) or a in (select distinct a from test)
john patrick active
john miles inactive
patrick john active
There's plenty of ways to solve this problem. 有很多方法可以解决此问题。 This is just one: 这只是一个:
SELECT *
FROM users
WHERE
(X = 'john' AND Y = 'patrick')
OR
(Y = 'john' AND X = 'patrick');
SELECT
X, Y, Z
FROM
TABLE AS a
JOIN
TABLE AS b
ON b.X = a.Y
AND b.Y = a.X
WHERE
(a.X = 'john' OR b.X = 'john')
Here's your requirements expressed as SQL: 这是表达为SQL的要求:
select a.X, a.Y, a.Z
from mytable a
join mytable b on a.X = b.Y and a.Y = b.X
where a.X = 'john';
This is very similar to ypercube's answer, but differs in one small but very important way: there is no need for or bY = 'john'
in the where clause, because this is logically covered by the on
clause of the join (the join says aX = bY
, so if aX = 'john'
we know already that bY = 'john'
). 这与ypercube的答案非常相似,但是有一个很小但很重要的区别:在where子句中不需要or bY = 'john'
,因为在逻辑上,这由join的on
子句覆盖(join表示aX = bY
,所以如果aX = 'john'
我们已经知道bY = 'john'
)。
This difference might seem small, but is large in terms of performance: Databases will not use indexes when OR
is used to match indexed columns, ie: 这种差异似乎很小,但是在性能方面却很大:当使用OR
匹配索引列时,数据库将不使用索引,即:
where col = 'x' or col = 'y' -- no: will not use index on col
where col = 'x' -- yes: will use index on col
My query will use an index on column X if one exists). 我的查询将使用X列上的索引(如果存在)。 If one doesn't exist, create one. 如果不存在,请创建一个。
Incidentally, to get the database to use an index in an OR
on a given column, use IN
instead. 顺便说一句,要使数据库在给定列的OR
使用索引,请改用IN
。 These next two lines are logically identical, but perform very differently 接下来的两行在逻辑上是相同的,但执行方式却大不相同
where col = 'x' or col = 'y' -- no: will not use index on col
where col in ('x', 'y') -- yes: will use index on col
您不需要加入。
select X,Y,Z from users where X like '%john%' or Y like '%john%';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.