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 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

如果第一个查询没有结果,则将执行第二个查询。
有没有办法在一个查询中做到这一点?

4

5 回答 5

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
                                      ))
于 2013-05-23T14:58:52.093 回答
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)
于 2013-05-23T14:53:00.177 回答
0

使用或逻辑

and offers.idhost = 1
and 
(offers.id in (that subquery)
or
Products.partnerObject = 0
)
于 2013-05-23T14:53:16.583 回答
0

为什么不尝试 OR 子句?

由于 SELECT、FROM AND WHERE 子句相同,请在括号中尝试 OR 子句

于 2013-05-23T14:54:15.357 回答
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
    )
)
于 2013-05-23T15:24:32.377 回答