动态在Sql Select Query中的位置

时间:2013-02-01 10:15:37

标签: sql sql-server where

我想创建一个像这样的SQL查询:

Select* from Table where 
(if picod=1)
{
  dvdt= "xxxx" 
}
(if picod=2)
{
  cddt= "xxxx" 
}
(if picod=3)
{
  bldt= "xxxx" 
}
(if picod=3)
{
  fadt= "xxxx" 
}

我不知道如何在SQL中这样做。

有人可以帮我吗?

非常感谢:)

3 个答案:

答案 0 :(得分:4)

Select* from Table 
where (picod=1 and dvdt= 'xxxx') or  (picod=2 and cddt= 'xxxx') or ....... (XXXX) or....

答案 1 :(得分:3)

只需使用OR

即可
SELECT  *
FROM    Table
WHERE   (Picod = 1 AND dvdt = 'xxxx')
OR      (Picod = 2 AND cddt = 'xxxx')
OR      (Picod = 3 AND bldt = 'xxxx')
OR      (Picod = 3 AND fadt = 'xxxx');

答案 2 :(得分:0)

 SELECT  *
    FROM    Table
    WHERE  'xxxx' = case Picod  
     when 1 then dvdt
     when 2 then cddt
     when 3 then bldt 
     when 4 then fadt 
    end