我正在尝试使用 UNION ALL 连接 3 个表。我尝试了以下代码。并给出无效参数编号的错误:绑定变量的数量与标记的数量不匹配。$codeArray = explode(',', $code);$inQuery = implode(',', array_fill(0, count($codeArray), '?')); $full_dt = date('Y-m-d H:i:s');$query = "SELECT * FROM ( SELECT a.* FROM pat_info a INNER JOIN pat_medication b ON a.id = b.pat_id WHERE a.status != 2 AND b.status != 2 AND '$full_dt' BETWEEN b.start_date AND b.end_date AND a.location_code IN ($inQuery) AND b.stock_status != '2' AND (b.total_qty - (b.given + b.not_taken)) < 12 UNION ALL SELECT a.* FROM pat_info a INNER JOIN prn_medication b ON a.id = b.pat_id WHERE a.status != 2 AND b.status != 2 AND '$full_dt' BETWEEN b.start_date AND b.end_date AND a.location_code IN ($inQuery) AND b.stock_status != '2' AND (b.total_qty - (b.given + b.not_taken)) < 12 ) x GROUP BY a.id ORDER BY a.id DESC";$statement = $con->prepare($query);$statement->execute($codeArray);
1 回答
Helenr
TA贡献1780条经验 获得超3个赞
由于代码中存在in
两次该子句,因此需要将值绑定两次。
execute()
执行此操作的一个简单方法是在...之前复制数据。
$codeArray = array_merge($codeArray, $codeArray);
你还需要改变
GROUP BY a.id ORDER BY a.id DESC
到
GROUP BY x.id ORDER BY x.id DESC
因为a
别名位于子选择中,而不是整个选择中。
- 1 回答
- 0 关注
- 92 浏览
添加回答
举报
0/150
提交
取消