代码中 where and 拼sql小技巧如下:
方式一:
$sql = "where true";
#或
$sql = "where 1 = 1";
其他条件就可以使用and 来连接sql
$sql .= "and a = 'a' ";
$sql .= "and b = 'b' ";
$sql .= "and c = 'c' ";
注:where 拼接的第一个字段的要求:表达式结果为true,且不影响返回结果
方式二:
可以把要拼接的条件放到list里,然后使用" and "字符串join
$where_list = [
" a = 'a' ",
" b = 'b' ",
" c = 'c' ",
];
$where_sql = "where ". $where_list.join(" and ");