2016-05-16 32 views
0

假设有一个表如何选择一行如果条件为true,所有行如果在sql中条件为false?

临时

COL1 | col ----

100 | c

456 | C1

131 | C2

-

假设我有coll1 = 100表示​​有效行的查询必须只返回行
如果值不表查询存在必须返回所有的行从表中。

注:如果存在,或任何程序上的东西(仅适用于SQL没有TSQL)

我们不能用下面

if exists(select 1 from temp where col1=100) 

     begin 
     select 1 from temp where col1=100 
    end 
    else 
    begin 
     select * from temp where col1=100 
    end 

回答

0

您可以只使用not exists做到这一点,我们不能使用:

SELECT * 
FROM temp t 
WHERE col1 = 100 
    OR NOT EXISTS (SELECT 1 
        FROM temp 
        WHERE col1 = 100); 
1
$qry1="Select Col1 from table where col1=100"; 
$res1 = mysql_query($qry1); 
if(row_count($res)>0){ 
/* Process data*/ 
} 
else{ 
$qry2="Select * from table where col1=100"; 
$res2 = mysql_query($qry2); 
/*process data*/ 
} 
+1

我们有先决条件,我们不能使用如果块和只有SQL –