Home > Backend Development > PHP Tutorial > SQL查询语句:select * from Table wher A B C D

SQL查询语句:select * from Table wher A B C D

WBOY
Release: 2016-06-13 10:02:05
Original
1054 people have browsed it

求一个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 ";
}
依次类推

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template