日期:2014-05-17  浏览次数:20443 次

求一个SQL查询语句:select * from Table wher A B C D
select   *   from   Table   wher   A   and   B   and     C   and   D

其中,A,B,C,D为条件。应该怎样构造啊?

是不是要先判断a,b,c,d中,只要有一个不为空,则“where”

那AND呢?》

------解决方案--------------------
判断非空,拼sql字符串.一般先加 where 1=1 ,不会出现where and A了
------解决方案--------------------
hookee() 正解
------解决方案--------------------
hookee() 正解

where 1=1 和 where 1 != 1 在sql中会经常用到

1=1 为后面加东西做好铺垫,1 != 1 得到表的结构
------解决方案--------------------
$aWhere[] = " 1=1 ";
if(!empty($A))
$aWhere[] = $A;
if(!emtpy($B))
$aWhere[] = $B;
if(!empty($C))
$aWhere[] = $C;
if(!empty($D))
$aWhere[] = $D;

$sWhere = join( ' and ', $aWhere);
$sSql = "select * from tbl where $sWhere order by id desc ";

------解决方案--------------------
$condition = " ";
$link_symbol = " ";
if($a)
{
$condition .= "$link_symbol $a ";
$link_symbol = "and ";
}
依次类推