mysql不等于不工作

标签 mysql sql

我有一个大问题,我不知道发生了什么。这是我的

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

中的 invoice_numbers

这是我的 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/

相关文章:

MySQL 触发器需要调整

php+mysql,使用 "join"时出现汉字乱码

php - 为动态创建的列MYSQL选择变量

java - 在 Java 中执行 SQL 时处理所有异常

mysql - 如何计算按属性分组的行,该属性在特定日期之前未出现?

sql - PostgreSQL,从 2 个表中选择,但仅从表 2 中选择最新元素

PHP:调用 file.php 中非对象的成员函数 fetch_assoc()

java - java JDBC +mysql数据库更新查询

MySql 触发更新、插入和删除

c# - varchar 列自行排序