Home  >  Article  >  Backend Development  >  多个查询条件的sql话语的拼写技巧,求指点

多个查询条件的sql话语的拼写技巧,求指点

WBOY
WBOYOriginal
2016-06-13 12:38:39822browse

多个查询条件的sql语句的拼写技巧,求指点。
多个查询条件的sql语句的拼写技巧,求指点。

<br />
$sql="select * from tb1";<br />
if($id=$_GET['id'])<br />
{<br />
	$where.=" where id like "%$id%"";<br />
}<br />
if($name=$_GET['name'])<br />
{<br />
	$where.=" where name like "%$name%"";<br />
}<br />
<br />
//当id有值的时候<br />
sql=select * from tb1 where id like "%$id%"<br />
//当name有值的时候<br />
sql=select * from tb1 where name like "%$name%"<br />
<br />
//当同时又值的时候,sql就出错了<br />
sql=select * from tb1 where name like "%$name%" where where id like "%$id%" <br />
<br />
//当然你可以说用<br />
if($id=$_GET['id']&&$name=$_GET['name'])<br />
{<br />
	where.= "and";<br />
}<br />

我举的例子只有两个条件,实际项目中我这里有十几个条件,这种方式肯定不行。
求更好的拼接方式

Statement:
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