简体   繁体   English

选择具有相同外键但另一列具有一组值的行

[英]Select rows with the same foreign key but another column has a set of values

I have designed a database that stores data in a format like this where both columns are foreign keys leading to different tables. 我设计了一个数据库,该数据库以这种格式存储数据,其中两列都是通向不同表的外键。 This is a simplified version. 这是简化版本。

RNA_id | Experiment_id |
   1   |       a       |
   1   |       b       |
   2   |       a       |
   2   |       b       |
   2   |       c       |
   3   |       b       |
   4   |       a       |
   4   |       c       |

I want to select rows that have all three experiment IDs. 我想选择具有所有三个实验ID的行。 In this example the results should be 在此示例中,结果应为

RNA_id | Experiment_id |
   2   |       a       |
   2   |       b       |
   2   |       c       |

I tried 我试过了

GROUP BY RNA_id HAVING COUNT (DISTINCT Experiment_id)=3

but that just resulted in me having a seemingly random assortment of rows. 但这只是导致我的行看似随机而已。

The database is already quite large and my query as is is fairly complex already, additionally, I may possibly want to expand to 4 or more Experiment_ids. 数据库已经很大,而且我的查询已经相当复杂,此外,我可能想扩展到4个或更多的Experiment_id。

Here is a sanitized version of my query as it is complicated and I don't want to explain my whole data structure 这是查询的经过整理的版本,因为它很复杂,我不想解释我的整个数据结构

My existing query is: 我现有的查询是:

SELECT RNA_id, Experiment_id, <data values>
FROM data
LEFT JOIN ref1
LEFT JOIN ref2
LEFT JOIN ref3
LEFT JOIN ref4
WHERE <required data parameters>
ORDER BY RNA_id
LIMIT 0,5000;

Returns about 700 values, as it should, but when I change ORDER BY to that GROUP BY command earlier, it returns 9 values all of which have unique RNA_ids and there should be about 100 that get returned. 按应返回大约700个值,但是当我较早地将ORDER BY更改为该GROUP BY命令时,它返回9个值,所有这些值都具有唯一的RNA_id,应该返回大约100个值。

My RNA_id is actually 3 columns so I may just have to redo the entire database to get any of these solutions to work. 我的RNA_id实际上是3列,因此我可能只需要重做整个数据库就可以使用这些解决方案。

My Solution 我的解决方案

I figured out how to make it work with the 3 column identifier 我想通了如何使其与3列标识符一起使用

AND (RNA_id1, RNA_id2, RNA_id3) IN (SELECT RNA_id1, RNA_id2, RNA_id3
FROM data
WHERE <parameter>
GROUP BY RNA_id1, RNA_id2, RNA_id3
HAVING COUNT (DISTINCT Experiment_id)=3)

This works with different numbers of Experiment_ids too 这也适用于不同数量的Experiment_id

I would recommend just returning RNA_id that have all three: 我建议只返回具有所有三个的RNA_id

select RNA_id
from t
where Experiment_id in ('a', 'b', 'c')
group by RNA_id
having count(*) = 3;

If you can have duplicates, then use count(distinct experiment_id) . 如果可以重复,则使用count(distinct experiment_id)

If you want the original rows, then in MySQL 8+, you can adapt this using window functions: 如果需要原始行,则在MySQL 8+中,可以使用窗口函数来调整它:

select t.*
from (select t.*, count(*) over (partition by RNA_id) as cnt
      from t
      where Experiment_id in ('a', 'b', 'c')
     ) t
where cnt = 3;

SQL DEMO SQL演示

SELECT *
FROM Table1 t1
WHERE ( SELECT COUNT(DISTINCT `Experiment_id`)
        FROM Table1 t2
        WHERE t2.`RNA_id` = t1.`RNA_id`
          AND t2.Experiment_id in ('a', 'b', 'c') -- if you have more than 3 experiment
      )  = 3

If a , b and c are the only possible values for Experiment_id , then you need to put your query in the WHERE clause like this: 如果abcExperiment_id的唯一可能值,则需要将查询放入WHERE子句中,如下所示:

select *
from tablename
where RNA_id in (
  select RNA_id from tablename
  group by RNA_id
  having count(distinct Experiment_id) = 3
)

If there are other values also: 如果还有其他值:

select *
from tablename
where
  Experiment_id in ('a', 'b', 'c') 
  and RNA_id in (
  select RNA_id from tablename
  where Experiment_id in ('a', 'b', 'c')
  group by RNA_id
  having count(distinct Experiment_id) = 3  
)

These queries can be easily changed if you want to expand to more than 3 values. 如果要扩展到三个以上的值,可以轻松更改这些查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除具有相同外键但具有不同值的行 - Removing rows with same foreign key but with different values Mysql选择一列中具有相同值而另一列中具有不同值的行 - Mysql Select rows with same values in one column and different in another 使用column1中的相同外键和column2中的不同值将多行插入到表中 - Inserting multiple rows into a table with the same foreign key in column1 & different values for column2 选择具有相同列值的行 - select rows with the same column values 在MySQL中,如何获取相同外键的所有值都显示在另一个列的列表中的记录 - In Mysql how to get records which for the same foreign key has all values showed up in a list for another col Django在同一记录中填充值,该记录之前已设置了外键 - Django populating values in the same record whose foreign key has been set before 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column 选择所有具有相同外键的行都满足条件的位置 - select where all rows having same foreign key, meet the condition 根据另一列的字段值选择具有相同列值的行 - Select rows that have the same column value based on another column's field values 如何在一列中选择具有相同值但在另一列中所有列必须彼此不同的行 - How to select rows with same values in one column but all must be different between each other in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM