首页 > 解决方案 > SELECT where not exists with another select 语句

问题描述

我在这里有这段代码:

select wms.product_code, WMS.barcode from(Select id,barcode, 
concat('0',barcode,checkdigit) as zc from outerb) as outerb 
join wms on outerb.zc = wms.barcode;

我需要根据上面的查询选择/查看所有不匹配的值。

这是我已经尝试过的:

SELECT * FROM wms where not exists (select wms.product_code, WMS.barcode 
from(Select id,barcode, concat('0',barcode,checkdigit) as zc from outerb) as 
outerb 
join wms on outerb.zc = wms.barcode);

但是这个查询什么也不返回。该zc表也是临时表。

使用 MySql 工作台

Table: outerb
Columns:
Id int(11) AI PK 
Product_code varchar(255) 
Brand varchar(255) 
Product_desc varchar(255) 
Size varchar(255) 
Barcode varchar(255) 
checkdigit varchar(255)

Table: wms
Columns:
Id int(11) AI PK 
Product_code varchar(255) 
Barcode varchar(255)

标签: mysql

解决方案


这将为您提供在 OUTERB 中没有匹配的所有 WMS 行:

select *
from wms
where not exists
(
  select null
  from outerb o
  where concat('0', o.barcode, o.checkdigit) = wms.barcode
);

或使用NOT IN

select *
from wms
where barcode not in (select concat('0', o.barcode, o.checkdigit) from outerb);

推荐阅读