0

我有一个 SELECT 查询

SELECT bill.accountid, bill.price, bill.id, price.inclusive, price.exclusive
from bill inner join price on bill.id = price.id
where bill.price <> 
CASE bill.id WHEN 1 THEN price.Inclusive
CASE bill.id WHEN 2 THEN price.Inclusive
CASE bill.id WHEN 3 THEN price.exclusive

如何在没有单独功能的情况下实现这个 where 子句

谢谢

4

3 回答 3

1

为什么不直接使用这个,一个在不同值的标准之间WHERE有一个的子句:ORbill.id

SELECT bill.accountid, 
  bill.price, 
  bill.id, 
  price.inclusive, 
  price.exclusive
from bill 
inner join price 
  on bill.id = price.id
where 
(
  bill.id IN (1, 2)
  and bill.price <> price.Inclusive
)
OR
(
  bill.id = 3
  and bill.price <> price.exclusive
)
于 2012-11-01T11:59:22.263 回答
0
SELECT
  b.accountid, 
  b.price, 
  b.id, 
  p.inclusive, 
  p.exclusive
FROM bill b
INNER JOIN price p ON b.id = p.id
WHERE b.price <> p.Inclusive
  AND b.id IN (1, 2, 3)
于 2012-11-01T11:58:47.647 回答
0

我相信 bill.id 已编入索引,并且可以使用条件语句有效地访问它,

您的查询无法使用 bill.id 索引,因为它使用 bill.id 计算原始值。

所以,我的回答是,将你的结果集分成更小的并直接比较。

(使用或条件也是很好的答案。)

SELECT bill.accountid, bill.price, bill.id, price.inclusive, price.exclusive
from bill inner join price on bill.id = price.id
where bill.id in (1, 2) and bill.price <> price.Inclusive
UNION ALL
SELECT bill.accountid, bill.price, bill.id, price.inclusive, price.exclusive
from bill inner join price on bill.id = price.id
where bill.id = 3 and bill.price <> price.exclusive;
于 2012-11-01T12:09:26.417 回答