不等于案例陈述SQL的结果

时间:2012-11-01 11:55:33

标签: sql

我有一个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子句

由于

3 个答案:

答案 0 :(得分:1)

为什么不使用这个WHERE子句,OR在不同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
)
OR
(
  bill.id = 3
  and bill.price <> price.exclusive
)

答案 1 :(得分: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)

答案 2 :(得分: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;