SQL 除或减
SQL Except or Minus
我有两个 table 相同的 "id" 公司,我需要 "select all" 制作减去特殊公司,在具体日期开具发票。
我试过 minus 功能,但它在我们的 ERP 系统中不起作用,但我可以使用 "except",但它无法按我需要的方式工作。
SELECT ad.idfirmy FROM aadresar as ad .............\ (select all)
EXCEPT
SELECT DISTINCT f.idfirmy FROM ddoklfak as f ...... \ (this i need minus from all sellect)
WHERE modul = 'FAV' and f.datvyst >= '<<datum_od>>' and f.datvyst <=
'<<datum_do>>') db on db.idfirmy = ad.idfirmy
例如 我有 "idfirmy value 193451" 排在第二 select 之后 except 仍然是结果中的这个值但是它太糟糕了。
我需要从第一个
中减去第二个select
我建议您删除 db
部分
SELECT ad.idfirmy
FROM aadresar AS ad
EXCEPT
SELECT DISTINCT f.idfirmy
FROM ddoklfak AS f
WHERE modul = 'FAV' AND f.datvyst >= '<<datum_od>>'
AND f.datvyst <= '<<datum_do>>';
或改用NOT EXISTS
SELECT ad.idfirmy
FROM aadresar AS ad
WHERE NOT EXISTS
(
SELECT 1
FROM ddoklfak AS f
WHERE f.idfirmy = ad.idfirmy
AND modul = 'FAV'
AND f.datvyst >= '<<datum_od>>'
AND f.datvyst <= '<<datum_do>>'
);
我有两个 table 相同的 "id" 公司,我需要 "select all" 制作减去特殊公司,在具体日期开具发票。
我试过 minus 功能,但它在我们的 ERP 系统中不起作用,但我可以使用 "except",但它无法按我需要的方式工作。
SELECT ad.idfirmy FROM aadresar as ad .............\ (select all)
EXCEPT
SELECT DISTINCT f.idfirmy FROM ddoklfak as f ...... \ (this i need minus from all sellect)
WHERE modul = 'FAV' and f.datvyst >= '<<datum_od>>' and f.datvyst <=
'<<datum_do>>') db on db.idfirmy = ad.idfirmy
例如 我有 "idfirmy value 193451" 排在第二 select 之后 except 仍然是结果中的这个值但是它太糟糕了。
我需要从第一个
中减去第二个select我建议您删除 db
部分
SELECT ad.idfirmy
FROM aadresar AS ad
EXCEPT
SELECT DISTINCT f.idfirmy
FROM ddoklfak AS f
WHERE modul = 'FAV' AND f.datvyst >= '<<datum_od>>'
AND f.datvyst <= '<<datum_do>>';
或改用NOT EXISTS
SELECT ad.idfirmy
FROM aadresar AS ad
WHERE NOT EXISTS
(
SELECT 1
FROM ddoklfak AS f
WHERE f.idfirmy = ad.idfirmy
AND modul = 'FAV'
AND f.datvyst >= '<<datum_od>>'
AND f.datvyst <= '<<datum_do>>'
);