101

我需要根据某种优先级从表中选择数据,如下所示:

select product, price from table1 where project = 1

-- pseudo: if no price found, do this:
select product, price from table1 where customer = 2

-- pseudo: if still no price found, do this:
select product, price from table1 where company = 3

也就是说,如果我发现 3 个产品的价格基于project = X,我不想选择customer = Y。我只想返回结果 3 行并完成。

你应该如何在 SQL 中做这样的事情?对伪 if 使用某种 CASE 语句?做工会或其他一些聪明的事情?

编辑:我正在使用 MS SQL。

谢谢!

4

7 回答 7

109

您可以进行以下sql查询

IF ((SELECT COUNT(*) FROM table1 WHERE project = 1) > 0) 
    SELECT product, price FROM table1 WHERE project = 1
ELSE IF ((SELECT COUNT(*) FROM table1 WHERE project = 2) > 0) 
    SELECT product, price FROM table1 WHERE project = 2
ELSE IF ((SELECT COUNT(*) FROM table1 WHERE project = 3) > 0)
    SELECT product, price FROM table1 WHERE project = 3
于 2013-02-26T09:57:36.863 回答
31

CASE 语句最接近 SQL 中的 IF 语句,所有版本的 SQL Server 都支持:

SELECT CASE <variable> 
           WHEN <value>      THEN <returnvalue> 
           WHEN <othervalue> THEN <returnthis> 
           ELSE <returndefaultcase> 
       END 
  FROM <table> 
于 2013-02-26T09:56:43.237 回答
17

而不是使用EXISTSCOUNT只是使用@@ROWCOUNT

select product, price from table1 where project = 1

IF @@ROWCOUNT = 0
BEGIN
    select product, price from table1 where customer = 2

    IF @@ROWCOUNT = 0
    select product, price from table1 where company = 3
END
于 2013-02-26T10:00:47.043 回答
2

使用 SQL Server,您可以只使用 CTE 而不是 IF/THEN 逻辑,以便轻松映射现有查询并更改涉及的查询数量;

WITH cte AS (
    SELECT product,price,1 a FROM table1 WHERE project=1   UNION ALL
    SELECT product,price,2 a FROM table1 WHERE customer=2  UNION ALL
    SELECT product,price,3 a FROM table1 WHERE company=3
)
SELECT TOP 1 WITH TIES product,price FROM cte ORDER BY a;

一个用于测试的 SQLfiddle

或者,您可以将它们组合成一个SELECT以简化优化器;

SELECT TOP 1 WITH TIES product,price FROM table1 
WHERE project=1 OR customer=2 OR company=3
ORDER BY CASE WHEN project=1  THEN 1 
              WHEN customer=2 THEN 2
              WHEN company=3  THEN 3 END;

另一个 SQLfiddle

于 2013-02-26T10:26:23.990 回答
1

请检查这是否有帮助:

select TOP 1
    product, 
    price 
from 
    table1 
where 
    (project=1 OR Customer=2 OR company=3) AND
    price IS NOT NULL
ORDER BY company 
于 2013-02-26T10:05:04.757 回答
0

有一个案例陈述,但我认为以下内容更准确/有效/更容易阅读您想要的内容。

select 
  product
  ,coalesce(t4.price,t2.price, t3.price) as price
from table1 t1
left join table1 t2 on t1.product = t2.product and t2.customer =2
left join table1 t3 on t1.product = t3.product and t3.company =3
left join table1 t4 on t1.product = t4.product and t4.project =1
于 2013-02-26T10:02:46.053 回答
0

- 大部分情况与上述类似的答案。包含用于测试的代码

DROP TABLE table1
GO
CREATE TABLE table1 (project int, customer int, company int, product int, price money)
GO
INSERT INTO table1 VALUES (1,0,50, 100, 40),(1,0,20, 200, 55),(1,10,30,300, 75),(2,10,30,300, 75)
GO
SELECT TOP 1 WITH TIES product
        , price
        , CASE WhereFound WHEN 1 THEN 'Project'
                WHEN 2 THEN 'Customer'
                WHEN 3 THEN 'Company'
            ELSE 'No Match'
            END AS Source
FROM 
    (
     SELECT product, price, 1 as WhereFound FROM table1 where project = 11
     UNION ALL
     SELECT product, price, 2 FROM table1 where customer = 0
     UNION ALL
     SELECT product, price, 3 FROM table1 where company = 30
    ) AS tbl
ORDER BY WhereFound ASC
于 2014-02-26T21:45:00.713 回答