0

我在使此查询正常工作时遇到问题,我实际上是在尝试同时使用 OR 和 AND 运算符,但似乎无法弄清楚为什么会出现此错误的问题“:"expecting绝对'或ACTION' or添加'或ALL' or更改' “`。这是我的sql代码:

SELECT * 
FROM EDXW ED, TRANSACTION TXN
WHERE ED.LAM_BK like 'TAR%'
AND ED.KEY = TXN.KEY
AND substring(ED.JAM_BK from position('~' in ED.JAM_BK) + 1) = TXN.EBS_ID

(AND ED.CLS_CD IN ('WIS','OP','OPP')
AND TXN.REV_CD IN ('0360','0361')
AND TXN.LS_CTR_CD IN ('548000','552000','552500','553000','553500','554000','555000','555500','558000')
AND ED.SITE_CD = 'X' )

OR

( ED.ED_CLS_CD IN ('WIS','OP','OPP')
AND AND TXN.REV_CD IN ('0350','0311')
AND AND TXN.LS_CTR_CD IN ('548000','558300')
AND LOC.ED_SITE_CD = 'M')

OR

( ED.ED_CLS_CD IN ('WIS','OP','OPP')
AND AND TXN.REV_CD IN ('0350','0311')
AND AND TXN.LS_CTR_CD IN ('549000','557300')
AND LOC.ED_SITE_CD IN ('M','K'));
4

2 回答 2

3

你有额外的括号或丢失和/或。这是显示问题的简化版本:

SELECT * 
FROM tables
WHERE clause1
(AND clause2)

从 SQL 的角度来看,这是不正确的。可能的正确版本是

SELECT * 
FROM tables
WHERE clause1
AND (clause2)

SELECT * 
FROM tables
WHERE clause1
OR (clause2)

看到不同?括号组语句。所以你可以有更复杂的表达式,比如: SELECT * FROM tables WHERE Clause1 AND (clause2 or Clause3)

他们的关键是,如果您要将括号中的所有内容减少为单个真/假值,则查询仍然有意义。

于 2013-10-19T17:58:30.843 回答
3

我认为问题在于你说:a and b and c and d or e or f但我认为你想说a and b and c and (d or e or f)的是非常不同的东西。当 f 为真或 e 为真时,整个表达式为真。大括号清楚地表明了这一点。我认为您的选择必须如下所示:

SELECT * 
FROM EDXW ED, TRANSACTION TXN
WHERE ED.LAM_BK like 'TAR%'
AND ED.KEY = TXN.KEY
AND substring(ED.JAM_BK from position('~' in ED.JAM_BK) + 1) = TXN.EBS_ID
AND
(
 (ED.CLS_CD IN ('WIS','OP','OPP')
 AND TXN.REV_CD IN ('0360','0361')
 AND TXN.LS_CTR_CD IN ('548000','552000','552500','553000','553500','554000','555000','555500','558000') 
 AND ED.SITE_CD = 'X' )

OR

 ( ED.ED_CLS_CD IN ('WIS','OP','OPP')
 AND TXN.REV_CD IN ('0350','0311')
 AND TXN.LS_CTR_CD IN ('548000','558300')
 AND LOC.ED_SITE_CD = 'M')

OR

 ( ED.ED_CLS_CD IN ('WIS','OP','OPP')
 AND TXN.REV_CD IN ('0350','0311')
 AND TXN.LS_CTR_CD IN ('549000','557300')
 AND LOC.ED_SITE_CD IN ('M','K'))
);
于 2013-10-19T18:08:52.303 回答