简体   繁体   English

如何选择内容相同但列不同的MySQL行?

[英]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.

相关问题 MySQL-排序具有相同列内容的行 - Mysql - Order rows that have same columns content 如何编辑此mySQL查询以不选择2列中具有相同值的行? - How to edit this mySQL query to not select rows that have same value in 2 columns? Mysql选择行两列不具有相同值 - Mysql Select Rows Where two columns do not have the same value 如何在MySQL的同一张表上的两个不同输入值之间的两个不同列之间选择行 - How to select rows between two different columns with two different input values on same table on mysql 如何添加两个不同行的两列在MySQL中具有相同的字段? - how to add two columns of two different rows have one same field in MySQL? 如何识别mysql中相同列中具有相同值的行? 而且我还必须向这些行添加条件 - How to identify rows that have the same value in same columns in mysql? and also i have to add condition to those rows 如何在 mysql 中的某些列中组合具有相同 ID 但不同值的行 - How to combine rows with same ID but different values in certain columns in mysql mysql 从结果,如何 select 行在 2 列中具有相同的值 - mysql from a result, how to select rows that have same values in 2 column 如何使用SQL / mySQL选择2列具有相同值的多个且1列具有不同值的行? - How do I use SQL/mySQL to select rows where 2 columns have multiple of the same value and 1 column has a distinct value? 比较同一列,不同行中的值-MySQL - Comparing values in same columns, different rows - MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM