2

早晨,

我无法弄清楚为什么我的查询(如下)忽略WHERE了 amzLive 的子句。并且仍在返回重复项。

SELECT  asin
FROM    dbo.aboProducts
WHERE  (asin NOT IN
             (SELECT DISTINCT productAsin
              FROM   dbo.LowestPrices
              WHERE (priceDate > GETDATE() - 1))) AND (amzLive = 'true')

它返回一个项目列表,但仍带回 amzLive 设置为 true 或 false 的项目。

有人可以看到我哪里出错了吗?

根据要求更新 数据库结构... aboProducts 表

id          int            Unchecked
asin        nvarchar(50)   Checked
codeType    int            Checked
sku         nvarchar(50)   Unchecked
amzPrice    decimal(18, 2) Checked
amzLive     bit            Checked
lastUpdated datetime       Checked

最低价格表

id           int            Unchecked
productAsin  nvarchar(50)   Unchecked
price        decimal(18, 2) Unchecked
postage      decimal(18, 2) Checked
priceDate    datetime       Unchecked
twAmzPrice   decimal(18, 2) Checked
4

3 回答 3

1

这将起作用

Select *
From 
(SELECT  asin,amzLive
FROM    dbo.aboProducts
WHERE  (asin NOT IN
             (SELECT DISTINCT productAsin
              FROM   dbo.LowestPrices
              WHERE (priceDate > GETDATE() - 1)
              )
         ) 
       )X
where X.amzLive = 'true'

也试试这个

SELECT ap.asin
FROM dbo.aboProducts ap
Left Join   (SELECT DISTINCT productAsin FROM dbo.LowestPrices  WHERE (priceDate > GETDATE() - 1))x
ON ap.asin  = x.productAsin
WHERE ap.amzLive = 'true' 
AND LEN(ap.asin) > 0 -- ap.asin IS NOT NULL
于 2012-09-04T10:02:25.880 回答
0
SELECT  asin
FROM    dbo.aboProducts
WHERE  (asin NOT IN
             (SELECT DISTINCT productAsin
              FROM   dbo.LowestPrices
              WHERE (priceDate > dateadd(day,-1,GETDATE()) ))) AND (amzLive = 'true')
于 2012-09-04T10:09:38.963 回答
0
  1. 不要将 'true' 和 'false' 与位一起使用,使用 1 或 0
  2. Aleft join将使您的查询更清晰、更可预测
  3. 仅供参考,SQL Server 有金钱数据类型

例如

select ASIN
from aboProducts
    left join lowestprices
        on aboProducts.asin = lowestprices.productAsin
        and lowestprices.pricedate > (GETDATE()-1)
where lowestprices.productAsin is null
and aboProducts.amzlive=1
于 2012-09-04T10:12:03.683 回答