简体   繁体   English

MySQL Join:如果一行满足条件,则忽略所有重复行

[英]MySQL Join: Omit ALL repeating rows IF one row meets a condition

Given the following example tables 给定以下示例表

TABLE_A
-----
 ID  
-----
  1
  2
  3
  4
  5

TABLE_B
---------------------------
 TABLE_A_ID        DETAIL
---------------------------
    1               val_x
    2               val_x
    2               val_y
    4               val_y
    5               val_other

I am doing a left join on these two tables and get the following output 我在这两个表上进行左联接,并获得以下输出

-------------------------------
   TABLE_A_ID        DETAIL
-------------------------------
       1              val_x
       2              val_y
       2              val_x
       3              null
       4              val_y
       5              val_other

This is as I expect from a left join. 这是我从左联接中所期望的。

The problem I have is that I also want to remove rows that have DETAIL = val_y AND ALL rows that have a repeating TABLE_A_ID IF any row in the group has a DETAIL = val_y 我的问题是,我也想删除具有行DETAIL = val_y并具有重复的所有行TABLE_A_ID如果组中的任何行有一个DETAIL = val_y

So the output I need is; 所以我需要的输出是

-------------------------------
   TABLE_A_ID        DETAIL
-------------------------------
       1              val_x
       3              null
       5              val_other

I have tried using GROUP_BY TABLE_A_ID and HAVING DETAIL != val_y but that doesn't seem to work. 我尝试使用GROUP_BY TABLE_A_IDHAVING DETAIL != val_y但这似乎不起作用。 I think for obvious reasons as GROUP_BY and HAVING are for aggregates and eliminating values that are less than or greater than right? 我认为出于明显的原因,例如GROUP_BYHAVING用于聚合并消除小于或大于对的值?

Is there a way to do this in MySQL or am I asking too much? 有没有办法在MySQL中做到这一点,还是我要求太多?

Note: These are EXAMPLE tables. 注意:这些是示例表。 They do not reflect a production system, so would appreciate no comments or answers outside the scope of the question and example - it just confuses things. 它们不反映生产系统,因此不希望在问题和示例范围之外发表评论或回答-这只会使事情变得混乱。

SELECT sub1.id table_a_id, sub1.detail
FROM (
    SELECT a.id, detail
    FROM table_a a
    LEFT JOIN table_b b on a.id = b.table_a_id) sub1
LEFT JOIN (
    SELECT DISTINCT table_a_id
    FROM table_b
    WHERE detail = 'val_y') sub2 
ON sub1.id = sub2.table_a_id
WHERE sub2.table_a_id IS NULL

The sub2 subquery finds all the IDs that meet your criteria for removal. sub2子查询查找满足删除条件的所有ID。 This is then joined with the original query to filter out those IDs. 然后将其与原始查询合并以过滤出这些ID。

DEMO 演示

You could use another LEFT JOIN to eliminate ids with a 'val_y' value; 您可以使用另一个LEFT JOIN消除具有“ val_y”值的ID;

SELECT a.id as table_a_id, b1.detail 
FROM table_a a
LEFT JOIN table_b b1 ON a.id = b1.table_a_id
LEFT JOIN table_b b2 ON a.id = b2.table_a_id AND b2.detail = 'val_y'
WHERE b2.detail IS NULL

An SQLfiddle to test with (updated with new sample data) 要测试的SQLfiddle (已更新新的示例数据)

Try this: 尝试这个:

   SELECT b.table_a_id, b.detail
   FROM table_a a JOIN table_b b ON a.id = b.id
   GROUP BY b.table_a_id
   HAVING COUNT( b.table_a_id ) == 1;

something like 就像是

select * from table_b where table_a_id not in (
    select TABLE_A_ID from TABLE_B where detail = 'val_y'
)

I think below will help you 我认为以下将对您有帮助

Select a.ID ,b.Detail
From Table1 a 
LEFT OUTER JOIN (SELECT id ,count(*) from Table2 group by id having count(*) =1 ) b
ON a.id = b.ID

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

相关问题 如果一行满足特定条件,则汇总行 - Aggregate rows if one row meets a specific condition 从一个表中获取所有行,前提是满足特定条件时从mysql中的另一个表满足 - Getting all rows from one table provided the certain condition meets from another table in mysql Mysql join,所有行都满足条件; 第二个查询 - 至少一行满足条件(部分匹配) - Mysql join where all rows meet condition; second query - where at least one row meet condition (partial match) 将转换应用于一行中符合条件的一组MySQL - Apply a transformation to a group of rows in one row meets a criteria MySQL 如果两个表中的任何一个满足条件,则MySQL JOIN - MySQL JOIN if either of the 2 tables meets condition Mysql 如果一行未通过连接条件,则连接查询不排除具有相同 ID 的行 - Mysql join query not excluding rows with same id if one row fails the join condition Mysql 连接表而不重复属于同一行的行 - Mysql join tables without repeating rows which belong to same row Mysql连接并回显所有行,并附带条件 - Mysql Join and echo all rows, with condition MySQL在有条件的情况下在一列中连接多行 - MySQL Join multiple rows in one column with condition 选择在某一列上排序的上一行满足某些条件的行 - Select rows where previous row under ordering on one column meets some condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM