You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中从MySQL筛选Trash≠1(含null值)的数据?

解决方案

要排除Trash字段值为1(包括该字段为NULL)的数据,核心过滤逻辑对应的SQL条件是:Trash != 1 OR Trash IS NULL。结合你现有代码,分两种场景处理:

场景1:如果all_fetch方法支持复杂WHERE条件(如表达式/OR逻辑)

直接在所有分支的$o_where数组中添加Trash的过滤规则:

// 处理订单状态筛选
$o_status = @$_GET['o_status'];
// 初始化基础过滤条件:排除已归档数据
$o_where = array('Trash != ?' => 1, 'Trash IS NOT NULL' => null);
// 若你的数据库类支持OR组合条件,可调整为:
// $o_where = array('OR' => array('Trash != 1', 'Trash IS NULL'));

if(isset($o_status) and !empty($o_status)){
    $o_where['order_s'] = $o_status;
}else if(isset($type) and !empty($type)){
    $o_where['status'] = 1;
    $o_where['order_show_off'] = "No";
}else{
    $o_where['status'] = 1;
}

// 查询数据
$cou_list = $con->all_fetch("orders", $o_where, " group by order_id, vendor_id order by id desc");

场景2:如果all_fetch仅支持简单键值对WHERE条件

这种情况需要手动拼接Trash的过滤逻辑到额外SQL片段中:

// 处理订单状态筛选
$o_status = @$_GET['o_status'];
$o_where = array();

if(isset($o_status) and !empty($o_status)){
    $o_where['order_s'] = $o_status;
}else if(isset($type) and !empty($type)){
    $o_where['status'] = 1;
    $o_where['order_show_off'] = "No";
}else{
    $o_where['status'] = 1;
}

// 拼接Trash过滤条件到额外SQL里
$extra_sql = " AND (Trash != 1 OR Trash IS NULL) group by order_id, vendor_id order by id desc";

// 查询数据
$cou_list = $con->all_fetch("orders", $o_where, $extra_sql);

关键注意点

  • 确保表字段名与代码中一致(注意MySQL大小写规则,尽量保持统一)
  • 优先使用参数绑定方式(如场景1中的Trash != ?)避免SQL注入风险
  • 测试Trash IS NULL的过滤效果,确认空值记录能被正确包含

内容的提问来源于stack exchange,提问作者Rahul tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:50:17