您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

mysql 多条件查询问题

2024/8/9 14:37:45发布32次查看
select * from subcatalog where name like '%$keywords%' and catalog = $catalog order by rank $rank limit $pages, $rows
上面的语句,怎么可以做到当 $catalog 这个值为空的时候,可以搜索其他的数据,现在这里catalog = $catalog 为空的时候列表就没有东西出来了
回复讨论(解决方案) 当 $catalog 为空时
 catalog = $catalog order
就变成
 catalog =  order
显然出现了语法错误
至少需要用引号将 $catalog 括起,写作 '$catalog'
不过这样还是有问题的:只能查询到 catalog 为空的记录
如果你不希望这样,应该在 $catalog 为空时,不写 catalog='$catalog'
把and catalog = $catalog 这个条件提出来单独写成一个变量,当$catalog为空的时候整个条件都为空。。。
思路这样子的,语法自己调整下:
if($catalog==''){ $tiaojian = '';}else{ $tiaojian = and catalog = $catalog;}$sql = select * from subcatalog where name like '%$keywords%' $tiaojian order by rank $rank limit $pages, $rows
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product