简体   繁体   English

如何选择另一个表中不存在的行

[英]How do I select rows which do not exists in another table

I want to create a report. 我想创建一份报告。

I have 2 tables : 我有2张桌子:

  1. student_list student_list
  2. transaction_list transaction_list

Here's the rows included in those table 这是这些表中包含的行

student_list student_list

Stud_ID | Name
1       | Cat
2       | Dog
3       | Rabbit

transaction_list transaction_list

Trans_ID | Stud_ID | Payment_Month
1        | 1       | January
2        | 1       | February

Now I want to select data from student_list which exists in transaction_list and combine it with data from student_list which do not exist in transaction_list where Payment_Month = "January" 现在我想从student_list中选择存在于transaction_list中的数据,并将其与student_list中的数据相结合,这些数据在transaction_list中不存在,其中Payment_Month =“January”

I have tried this query 我试过这个查询

SELECT Name, Trans_ID, Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
  ON t.Stud_ID = s.Stud_ID
WHERE Payment_Month = "January"
UNION
SELECT Name, Trans_ID, Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
  ON t.Stud_ID = s.Stud_ID
WHERE t.Stud_ID IS NULL

And I got this 而我得到了这个

Name   | Trans_ID | Payment_Month
Cat    | 1        | January
Dog    | -        | -
Rabbit | -        | -

But when I change the Payment_Month value in the query to "March", I got this 但是,当我将查询中的Payment_Month值更改为“March”时,我得到了这个

Name   | Trans_ID | Payment_Month
Dog    | -        | -
Rabbit | -        | -

Not as I want it, because I'd like this 不是我想要的,因为我喜欢这个

Name   | Trans_ID | Payment_Month
Cat    | -        | -
Dog    | -        | -
Rabbit | -        | -

Is there anyway I can get that? 无论如何我能得到那个吗?

SELECT Name, Trans_ID, Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
  ON (t.Stud_ID = s.Stud_ID AND Payment_Month = "January")
UNION
SELECT Name, Trans_ID, Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
  ON t.Stud_ID = s.Stud_ID
WHERE t.Stud_ID IS NULL

Try it like this. 试试吧。 By using WHERE you are filtering by the actual result your query delivers, so if your LEFT JOIN delivers null but you are filtering on that column the row is removed from the resultset. 通过使用WHERE您可以根据查询提供的实际结果进行过滤,因此,如果您的LEFT JOIN传递null但是您要对该列进行过滤,则会从结果集中删除该行。

By putting the month into the JOIN condition it will just return null on this column (on the columns added by the JOIN ) and keep the row in the resultset. 通过将月份置于JOIN条件中,它将在此列上返回null(在JOIN添加的列上)并将行保留在结果集中。

You are using an OUTER JOIN, so take advantage of it. 您正在使用OUTER JOIN,因此请充分利用它。

  • Outer joins return all of the rows on one side of the join regardless of whether there is a match or not. 无论是否匹配, Outer joins返回Outer joins一侧的所有行。
  • Therefore, when you run a PREDICATE on the ( outer ) QUERY , you are eliminating the rows that would return the values you still want to see. 因此,当您在( 外部QUERY上运行PREDICATE时,您将消除将返回您仍希望看到的值的行。

You could do something like the following: 您可以执行以下操作:

SELECT Name, B.Trans_ID, Stud_ID B.Payment_Month
FROM student_list A
LEFT OUTER JOIN (SELECT TRANS_ID, PAYMENT_MONTH, Stud_ID
                 FROM   transaction_list
                 WHERE  PAYMENT_MONTH = "January") B
ON A.Stud_ID = B.Stud_ID

It will return all the matches on ID where transaction_list is in January as well as the ones not matched in student_list . 它将返回1月份transaction_list所在的ID以及student_list未匹配的所有匹配项。 TO be fair, you might have been wanting to eliminate duplicates in your query, so you can stil accomplish this in the GROUP BY clause. 公平地说,您可能希望消除查询中的重复项,因此您可以在GROUP BY子句中完成此操作。

Try this;) 试试这个;)

SELECT Name, COALESCE(Trans_ID, '-') AS Trans_ID, COALESCE(Payment_Month, '-') AS Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
ON t.Stud_ID = s.Stud_ID
AND NOT EXISTS (
    SELECT 1 FROM transaction_list tl WHERE tl.Stud_ID = t.Stud_ID AND tl.Payment_Month = 'January'
)

SQLFiddle DEMO HERE SQLFiddle DEMO HERE

Thanks to @Philipp 感谢@Philipp

This is the code that worked for me : 这是适合我的代码:

SELECT Stud_Name, Trans_ID, Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
ON (t.Stud_ID = s.Stud_ID AND Payment_Month= "January")
UNION
SELECT Stud_Name, Trans_ID, Payment_Month
FROM student_list s
LEFT JOIN transaction_list t
ON t.Stud_ID = s.Stud_ID
WHERE t.Stud_ID IS NULL

Even if I change "January" to "March", it worked as well as I wanted. 即使我将“1月”改为“3月”,它也能像我想要的那样奏效。

暂无
暂无

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

相关问题 如何选择不同的行并交叉检查另一个表? - How do I select distinct rows and cross check in another table? MYSQL, SELECT 语法问题,我如何 select 表的行基于另一个表中的要求 - MYSQL, SELECT syntax problem, how do I select rows of a table based on a requirement in another table 我怎样才能 select 所有不与另一行 null 共享列值的行? - How can I select all the rows which do not share a column value with another row which is null? 如果我知道另一个表中的值但不知道ID,如何从表中选择行? - How do I select rows from a table if I know value in another table but not ID? 如何从一个表中选择被MySQL中的另一个表过滤的行? - How do I select rows from one table filtered by another table in MySQL? 如何从一个表中选择行作为CSV并根据键插入另一个表? - How do I select rows from one table as CSV and insert into another table based on a key? MYSQL 我如何 select 数据同时具有另一个表的 WHERE EXISTS 条件和主表中的正常 WHERE 条件? - MYSQL How do I select data with both a WHERE EXISTS condition for another table and a normal WHERE condition in the main table? 在mysql中,我如何只从一个表中获取行,而该行不链接到具有特定ID的另一表中的任何行 - In mysql how can I get only rows from one table which do not link to any rows in another table with a specific ID 如何在MySQL的另一个表中仅选择没有相应外键的行? - How do I select only rows that don't have a corresponding foreign key in another table in MySQL? 我如何从 MySQL 表中的 select 行按一列分组,另一列具有所需值 - How do I select rows from a MySQL table grouped by one column with required values in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM