如何在SQL中使用“重复”条件进行选择?

时间:2011-12-21 05:32:24

标签: sql windows

我有以下选择需要完成:

  

从产品中选择前1 a,b,c,d   CodDep = 10或
  CodDep = 11或
  CodDep = 12或   CodDep = 13或
  CodDep = 26或
  CodDep = 27或   CodDep = 32或
  CodDep = 34或
  CodDep = 248442或
  CodDep = 259741   按LastUpdate排序

如果没有这么多重复,有没有一种简单的方法可以做到这一点?

3 个答案:

答案 0 :(得分:5)

select top 1 
  a, b, c, d 
from 
  Products 
where
  CodDep IN (10,12,12,13,26,27,32,34,248442,259741)
order by 
  LastUpdate

答案 1 :(得分:0)

尝试此查询:

SELECT TOP 1 a, b, c, d FROM Products WHERE
CodDep IN (10,11,12,13,26,27,32,34,248442,259741) ORDER BY LastUpdate 

答案 2 :(得分:0)

SQL只有一个数据结构:表。将值放在表格中,semi join放到该表格中,例如

WITH Params
     AS 
     (
      SELECT * 
        FROM (
              VALUES (10), (11), (12), (13),
                     (26), (27), (32), (34), 
                     (248442), (259741)
             ) AS T (CodDep)
     )
SELECT TOP 1 a, b, c, d 
  FROM Products 
 WHERE CodDep IN (SELECT CodDep FROM Params)
 ORDER 
    BY LastUpdate;