在另一个选择语句不存在的情况下进行SELECT

时间:2019-03-18 13:30:48

标签: mysql

我在这里有此代码:

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 Workbench

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)

3 个答案:

答案 0 :(得分:1)

这将为您提供所有在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);

答案 1 :(得分:0)

您可以在下面尝试-

SELECT * FROM wms where not exists 
    (select 1 from outerb where concat('0',barcode,checkdigit) = wms.barcode) 
and wms.barcode like '0123%'

答案 2 :(得分:0)

您可以尝试:

SELECT * FROM wms where wms.barcode not in (select concat('0',barcode,checkdigit) from outerb);
相关问题