这里是我的几乎相同,直到最后一部分
SELECT Offers.ID
FROM Offers, Products
WHERE Offers.IDProduct = Products.ID
AND Offers.fromDate <= GETDATE()
AND GETDATE() <= Offers.toDate
AND Offers.active = 1
AND Offers.IDHost = 1
AND Offers.ID IN
(SELECT idobject FROM Multilingual
WHERE Multilingual.IDLanguage = 2
AND Multilingual.IDObjecttype = 30
AND Multilingual.active = 1)
第二个查询是
SELECT Offers.ID
FROM Offers, Products
WHERE Offers.IDProduct = Products.ID
AND Offers.fromDate <= GETDATE()
AND GETDATE() <= Offers.toDate
AND Offers.active = 1
AND Offers.IDHost = 1
AND Products.partnerObject = 0
如果第一个查询没有结果,则执行第二个查询 有没有办法在一个查询中执行此操作?
答案 0 :(得分:0)
试试这个:
with multilingualobjects (objectid)
as
(
SELECT distinct idobject FROM Multilingual
WHERE Multilingual.IDLanguage = 2
AND Multilingual.IDObjecttype = 30
AND Multilingual.active = 1
)
SELECT Offers.ID
FROM Offers o
inner join Products p
ON o.IDProduct = p.ID
left outer join multilingualobjects mo
on o.ID = mo.objectid
where o.fromDate <= GETDATE()
AND GETDATE() <= o.toDate
AND o.active = 1
AND o.IDHost = 1
and (mo.objectid is not null OR p.partnerobject = 0)
答案 1 :(得分:0)
使用或逻辑
and offers.idhost = 1
and
(offers.id in (that subquery)
or
Products.partnerObject = 0
)
答案 2 :(得分:0)
为什么不尝试OR子句?
由于SELECT,FROM和WHERE子句相同,请在括号中使用OR子句
答案 3 :(得分:0)
SELECT o.ID
FROM Offers o JOIN Products p ON o.IDProduct = p.ID
WHERE o.fromDate <= GETDATE()
AND GETDATE() <= o.toDate
AND o.active = 1
AND o.IDHost = 1
AND (p.partnerObject = 0 OR o.ID IN(
SELECT idobject
FROM Multilingual m
WHERE m.IDLanguage = 2
AND m.IDObjecttype = 30
AND m.active = 1
))
答案 4 :(得分:0)
SELECT Offers.ID
FROM Offers, Products
WHERE Offers.IDProduct = Products.ID
AND Offers.fromDate <= GETDATE()
AND GETDATE() <= Offers.toDate
AND Offers.active = 1
AND Offers.IDHost = 1
AND
(
Offers.ID IN
(SELECT idobject FROM Multilingual
WHERE Multilingual.IDLanguage = 2
AND Multilingual.IDObjecttype = 30
AND Multilingual.active = 1)
or
(
(SELECT idobject FROM Multilingual
WHERE Multilingual.IDLanguage = 2
AND Multilingual.IDObjecttype = 30
AND Multilingual.active = 1) = 0
and
Products.partnerObject = 0
)
)