简体   繁体   English

mySQL 连接表,但根据来自另一列的匹配条件排除具有特定 ID/值的所有行(条件仅包含在某些行中)

[英]mySQL Join Tables but exclude ALL rows with a certain ID/value based on matching criteria from another column (criteria contained in only some rows)

Very new to SQL so I don't even know how to phrase my question properly, and maybe that's why I can't seem to find an answer.对 SQL 来说非常新,所以我什至不知道如何正确表达我的问题,也许这就是我似乎找不到答案的原因。 What I am trying to do essentially is include or exclude all rows from a table by an ID column based on matching criteria from another column.我要做的基本上是根据另一列的匹配条件通过 ID 列从表中包含或排除所有行。 Instead what happens is only the rows with the criteria are included/excluded rather than all with the matching ID.相反,仅包含/排除具有条件的行,而不是所有具有匹配 ID 的行。

I think I am trying to essentially do what is asked in this question but I cannot use the "create temporary table" command in mySQL due to lack of permissions (I assume any other temporary table methods would also fail due to this reason...)我想我基本上是在尝试做这个问题中提出的问题,但是由于缺少权限,我不能在 mySQL 中使用“创建临时表”命令(我假设任何其他临时表方法也会因为这个原因而失败...... )

I have two tables:我有两张桌子:

batch_table批处理表

BatchNo批号 Info信息
100 100 BBB BBB
101 101 AAA AAA
102 102 AAA AAA

sample_table样本表

SampleNo样品编号 SampleType样品类型 BatchNo批号 Status地位
1 1 Batch_QC_1批次_QC_1 100 100 'pass' '经过'
2 2 Batch_QC_1批次_QC_1 100 100 'pass' '经过'
3 3 Batch_QC_2批次_QC_2 100 100 'pass' '经过'
4 4 Batch_QC_2批次_QC_2 100 100 'pass' '经过'
5 5 Regular常规的 100 100 'positive' '积极的'
6 6 Regular常规的 100 100 'positive' '积极的'
7 7 Regular常规的 100 100 'negative' '消极的'
8 8 Regular常规的 100 100 'negative' '消极的'
9 9 Batch_QC_1批次_QC_1 101 101 'pass' '经过'
10 10 Batch_QC_1批次_QC_1 101 101 'QC_1 FAIL' 'QC_1 失败'
11 11 Batch_QC_2批次_QC_2 101 101 'pass' '经过'
12 12 Batch_QC_2批次_QC_2 101 101 'pass' '经过'
13 13 Regular常规的 101 101 'positive' '积极的'
14 14 Regular常规的 101 101 'positive' '积极的'
15 15 Regular常规的 101 101 'negative' '消极的'
16 16 Regular常规的 101 101 'negative' '消极的'

I need to combine the batch_table and the sample_table, keeping only the "passing batches."我需要组合 batch_table 和 sample_table,只保留“通过的批次”。 Each batch is a key in the batch_table.每个批次都是 batch_table 中的一个键。 I need to check the contents of the sample_table to see if a batch has passed.我需要检查 sample_table 的内容以查看批次是否已通过。 Passing is defined as all rows of the Status column = 'pass' where the SampleType is 'Batch_QC_1' or 'Batch_QC_2.'通过定义为 Status 列 = 'pass' 的所有行,其中 SampleType 为 'Batch_QC_1' 或 'Batch_QC_2'。 I then want to include/exclude by the entire BatchNo.然后我想通过整个 BatchNo 包含/排除。 So in the example data because there is a single row (sampleNo = 10) where the Status column is not 'pass' and where the SampleType column is 'Batch_QC_1'.所以在示例数据中,因为有一行 (sampleNo = 10),其中 Status 列不是“pass”,SampleType 列是“Batch_QC_1”。 I would like to exclude all of the rows with that same BatchNo ('101') instead of just that one.我想排除所有具有相同 BatchNo ('101') 的行,而不仅仅是那个。

I have tried many things - most seem to drop only the rows that do not equal 'pass' and do drop the all the rows with that same matching BatchNo.我已经尝试了很多事情——大多数似乎只删除了不等于“通过”的行,并且确实删除了具有相同匹配 BatchNo 的所有行。

For example I tried something like this:例如,我尝试过这样的事情:

SELECT 
sample_table.SampleNo, sample_tables.SampleType, sample_table.BatchNo, sample_table.Status, batch_table.Info

FROM 
batch_table
JOIN sample_table on batch_table.BatchNo = sample_table.BatchNo 
WHERE sample_table.Status NOT IN ('QC_1_FAIL', 'QC_2_FAIL')

This returns all rows except for row/sampleNo 10 from the sample_table where the Status = 'QC_2_FAIL'.这将返回状态 = 'QC_2_FAIL' 的 sample_table 中除 row/sampleNo 10 之外的所有行。 What I would like is to exclude all of the rows with the same BatchNo as row 10 (or any row with the matching criteria).我想要排除与第 10 行具有相同 BatchNo 的所有行(或具有匹配条件的任何行)。

query result (single row excluded, SampleNo = 10)查询结果(排除单行,SampleNo = 10)

SampleNo样品编号 SampleType样品类型 BatchNo批号 Status地位 Info信息
1 1 Batch_QC_1批次_QC_1 100 100 'pass' '经过' BBB BBB
2 2 Batch_QC_1批次_QC_1 100 100 'pass' '经过' BBB BBB
3 3 Batch_QC_2批次_QC_2 100 100 'pass' '经过' BBB BBB
4 4 Batch_QC_2批次_QC_2 100 100 'pass' '经过' BBB BBB
5 5 Regular常规的 100 100 'positive' '积极的' BBB BBB
6 6 Regular常规的 100 100 'positive' '积极的' BBB BBB
7 7 Regular常规的 100 100 'negative' '消极的' BBB BBB
8 8 Regular常规的 100 100 'negative' '消极的' BBB BBB
9 9 Batch_QC_1批次_QC_1 101 101 'pass' '经过' AAA AAA
11 11 Batch_QC_2批次_QC_2 101 101 'pass' '经过' AAA AAA
12 12 Batch_QC_2批次_QC_2 101 101 'pass' '经过' AAA AAA
13 13 Regular常规的 101 101 'positive' '积极的' AAA AAA
14 14 Regular常规的 101 101 'positive' '积极的' AAA AAA
15 15 Regular常规的 101 101 'negative' '消极的' AAA AAA
16 16 Regular常规的 101 101 'negative' '消极的' AAA AAA

desired result (all rows where BatchNo = 101 excluded)期望的结果(排除 BatchNo = 101 的所有行)

SampleNo样品编号 SampleType样品类型 BatchNo批号 Status地位 Info信息
1 1 Batch_QC_1批次_QC_1 100 100 'pass' '经过' BBB BBB
2 2 Batch_QC_1批次_QC_1 100 100 'pass' '经过' BBB BBB
3 3 Batch_QC_2批次_QC_2 100 100 'pass' '经过' BBB BBB
4 4 Batch_QC_2批次_QC_2 100 100 'pass' '经过' BBB BBB
5 5 Regular常规的 100 100 'positive' '积极的' BBB BBB
6 6 Regular常规的 100 100 'positive' '积极的' BBB BBB
7 7 Regular常规的 100 100 'negative' '消极的' BBB BBB
8 8 Regular常规的 100 100 'negative' '消极的' BBB BBB

I tried using variations of left/right joins as well but I'm thinking that I the issue is more that I don't know how to structure the WHERE portion of the query properly.我也尝试使用左/右连接的变体,但我认为我的问题更多在于我不知道如何正确构建查询的 WHERE 部分。 I think a temporary table might work but I can't seem to do this because of permissions.我认为临时表可能有效,但由于权限,我似乎无法这样做。 If not possible with a standard WHERE clause then is it possible to use a temporary variable of sorts to create an additional pass/fail column?如果无法使用标准 WHERE 子句,那么是否可以使用临时变量来创建额外的通过/失败列? Thank you so much.太感谢了。

SELECT s.SampleNo, s.SampleType, s.BatchNo, s.Status, b.Info FROM sample_table s 
INNER JOIN batch_table b on s.BatchNo = b.BatchNo
WHERE s.BatchNo NOT IN (
           SELECT DISTINCT(BatchNo) FROM sample_table WHERE Status != 'pass' AND 
           SampleType IN ('Batch_QC_2', 'Batch_QC_1')
          );

暂无
暂无

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

相关问题 MySQL查询:当另一列中的值符合特定条件时,返回一列中具有特定值的所有行 - MySQL Query: Return all rows with a certain value in one column when value in another column matches specific criteria SQL 排除匹配所有多个条件的行 - SQL exclude rows matching all of multiple criteria 在mysql join,union&gt; =和&lt;=中计算符合条件的行 - counting rows matching criteria in mysql join , union >= and <=? MySQL-在列中最后一次出现特定值之后,选择所有符合条件的行 - MySQL - Select all rows matching criteria after the last occurrence of a specific value in a column 根据匹配条件连接表,并从Table1的总和中减去Table2.Seantity的数量。匹配行的数量 - Join tables based on matching criteria and Subtract the sum of Table2.Quantity from sum of Table1.Quantity for matching rows MySql根据列条件明确地连接行 - MySql joining rows distinctly based on column criteria MySQL-根据条件联接3个表 - MySQL - join 3 tables based on criteria MySQL-如何联接2个表,并且仅在满足条件时才保持匹配的行 - Mysql — how to join 2 tables and only keep matching row if criteria is met 查找不符合条件的所有行,MySQLi MySQL PHP - Find all rows not matching a criteria, MySQLi MySQL PHP 根据某些条件返回行,如果没有行符合条件,则返回mariadb sql中的所有行 - returning rows based on some criteria and if no row match the criteria then return all rows in mariadb sql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM