我有一个大问题,我不知道发生了什么。这是我的
table1
+-----+-----------------------+--------------+
| id | invoice_number | invoice_date |
+-----+-----------------------+--------------+
| 12 | 12536801244 | 2009-09-23 |
| 38 | 12585302890 | 2009-11-18 |
| 37 | 12584309829 | 2009-11-16 |
| 123 | 12627605146 | 2010-01-06 |
| 191 | 12663105176 | 2010-02-16 |
+-----+-----------------------+--------------+
这是我的第二个
table2
+-----+-----------------------+--------------+
| id | invoice_number | invoice_date |
+-----+-----------------------+--------------+
| 12 | 1t657801244 | 2009-09-23 |
| 20 | 12585302890 | 2009-11-18 |
| 37 | 1ss58430982 | 2009-11-16 |
| 103 | 12627605146 | 2010-01-06 |
| 121 | 12346310517 | 2010-02-16 |
+-----+-----------------------+--------------+
我想要的是,我得到了所有不在 table2
这是我的 SQL 查询。
select t2.invoice_number FROM table1 t1
JOIN table2 t2 ON t2.invoice_number != t1.inovice_number;
但是我得到了不同的结果。 SQL代码有什么问题吗?
最佳答案
Performance wise you can use Left Join or Not EXIST
尝试左连接
Select t2.invoice_number
FROM table1 t1
LEFT JOIN table2 t2 ON
t2.invoice_number = t1.inovice_number AND
t2.invoice_number IS NULL;
尝试不存在
Select t2.invoice_number
FROM table1 t1 Where Not Exist
(
SELECT NULL
FROM table2 t2
WHERE t2.invoice_number = t1.inovice_number
)
试试 Not IN
Select t2.invoice_number
FROM table1 t1
Where t1.inovice_number NOT IN
(
SELECT t2.inovice_number
FROM table2 t2
)
关于mysql不等于不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397580/