简体   繁体   English

MySQL:如何基于列删除多行记录的一行

[英]MySQL: How to Remove One Row of a Multi-Row Record Based on Column

If I have two tables that I'm joining and I write the most simple query possible like this: 如果我有两个表,我正在加入,我写的最简单的查询可能是这样的:

SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id

There are a few records who have multiple rows per ID because they have multiple employers, so t1 looks like this: 有一些记录每个ID有多行,因为它们有多个雇主,所以t1看起来像这样:

ID         Name         Employer
12345      Jerry        Comedy Cellar
12345      Jerry        NBC
12348      Elaine       Pendant Publishing
12346      George       Real Estate
12346      George       Yankees
12346      George       NBC
12347      Kramer       Kramerica Industries

t2 is linked with the similar IDs but with some activities that I'd like to see -- hence the SELECT * above. t2与相似的ID相关联,但有一些我想看的活动 - 因此上面的SELECT * Though I don't want multiple rows to return if the Employer column is "NBC" -- but everything else is good. 虽然如果Employer专栏是“NBC”,我不想要多行返回 - 但其他一切都很好。

The only other thing that matters here is that t2 is smaller than t1 , because t1 is everybody and t2 are only from people who did particular activities -- so some of the matches won't return anything from t2 , but I would still like them to be returned, hence the LEFT JOIN . 唯一重要的另一件事是t2小于t1 ,因为t1是每个人而t2只来自进行特定活动的人 - 所以有些匹配不会从t2返回任何东西,但我仍然会喜欢它们返回,因此LEFT JOIN

If I write the query like this: 如果我写这样的查询:

SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
WHERE Employer <> "NBC"

Then it removes Jerry and George completely -- when really all I want is for the NBC row to not be returned, but to return any other rows that are associated with them. 然后它完全删除了Jerry和George - 当我真正想要的只是NBC行不被返回,而是返回与它们相关联的任何其他行。

How can I write the query while joining t1 with t2 to return each row except for the NBC ones? 如何在使用t2加入t1时写入查询以返回除NBC之外的每一行? The ideal output would be all of the rows from t1 regardless if they match up with all of t2 except removing all of the rows with "NBC" as the employer in the return file. 理想的输出是来自t1所有行,无论它们是否与t2所有行匹配,除了在返回文件中删除所有以“NBC”作为雇主的行。 Basically the ideal here is to return the JOINs where they fit, but regardless remove the entire row for anybody with "NBC" as employer without removing their other rows. 基本上这里的理想是返回它们适合的JOIN,但无论如何将“NBC”作为雇主移除整行而不删除其他行。

The more I write about it, it seems like I should potentially just run a query prior to my JOIN to delete all the rows in t1 who have "NBC" as their employer and then run the normal query. 我写的越多,似乎我应该在我的JOIN之前运行一个查询来删除t1中以“NBC”为雇主的所有行,然后运行普通查询。

Basic subset filtering 基本子集过滤

You can filter either of the two merged (joined) subsets by extending the ON clause. 您可以通过扩展ON子句来过滤两个合并(已连接)子集中的任何一个。

SELECT    *
FROM      t1
LEFT JOIN t2
       ON t1.ID = t2.ID
      AND t2.Employer != 'NBC'

If you get null values now, and you don't want them, you'd add: 如果您现在获得null值,并且您不想要它们,则需要添加:

WHERE t2.Employer IS NOT NULL

extended logic: 扩展逻辑:

SELECT    *
FROM      t1
LEFT JOIN t2
       ON (t1.ID = t2.ID AND t2.Employer != 'NBC')
       OR (t2.ID = t2.ID AND t2.Employer IS NULL)

Using UNION 使用UNION

Basically, JOIN is for horizontal linking and UNION does vertical linking of datasets. 基本上, JOIN用于水平链接, UNION用于垂直链接数据集。

It merges to resultsets: the first without NBC, and the second (which is basically an OUTER JOIN ), adds everyone in t1 which is not part of t2. 它合并到结果集:第一个没有NBC,第二个(基本上是一个OUTER JOIN ),在t1中添加了不属于t2的每个人。

SELECT    *
FROM      t1
LEFT JOIN t2
       ON t1.ID = t2.ID
      AND t2.Employer != 'NBC'
UNION
SELECT    *
FROM      t1
LEFT JOIN t2
       ON t1.ID = t2.ID
      AND t2.Employer IS NULL

String manipulation in the resultset 结果集中的字符串操作

If you just want to remove NBC as a string, here is a workaround: 如果您只想删除NBC作为字符串,这是一个解决方法:

SELECT
    t1.*,
    IF (t2.Employer = 'NBC', NULL, t2.Employer) AS Employer
FROM t1
LEFT JOIN t2
    ON t1.id = t2.id

This basically replaces "NBC" by NULL 这基本上用NULL替换"NBC"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM