[英]How do I select rows which do not exists in another table
I want to create a report. 我想创建一份报告。
I have 2 tables : 我有2张桌子:
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
一侧的所有行。 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'
)
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.