SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous:Integrityconstraintviolationcolumnidinwhereclauseisambiguous
thinkphp模型连表查询报错,请在where条件字段前加入表名。
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous:Integrityconstraintviolationcolumnidinwhereclauseisambiguous.
// 我的优惠券
public function getMyCoupon($user, $params)
{
$page = empty($params['page']) ? 1 : $params['page'];
$pageSize = empty($params['pageSize']) ? 15 : $params['pageSize'];
$orderby = "id desc";
// 使用状态 status = 1
$useStatus = empty($params['used']) ? 0: trim($params['used']);
// 过期优惠券 status = 1
$couponStatus = empty($params['status']) ? 0: trim($params['status']);
//coupon_members 表
$where[] = ['coupon_members.id', 'gt', 0];
$where[] = ['coupon_members.uid', '=', $user['uid']];
$where[] = ['coupon_members.status', '=', $useStatus]; // 未使用 status = 0
// coupon 表
$where_coupon[] = ['coupon.id', '>', 0];
$where_coupon[] = ['coupon.status', '=', $couponStatus];
$total = CouponMembers::withJoin(['coupon'=>function($query) use ($where_coupon){
$query->where($where_coupon)->field('name');
}])
->where($where)
->count();
$user_coupon = [];
$list = CouponMembers::withJoin(['coupon'=>function($query) use ($where_coupon){
$query->where($where_coupon);
}])
->where($where)
->order($orderby)
->limit($pageSize)
->page($page)
->select();
// 格式化数据
$user_coupon = $list ? $list->toArray(): [];
// 门店记录
$shop = [];
// 初始化返回数据
$back_result = [];
if (!empty($user_coupon)) {
$coupon_ids = [];
foreach($user_coupon as $item) {
$coupon_ids[] = $item['coupon']['shop_id'];
}
// 查询门店信息
$shopModel = new Shop();
$shopInfo = $shopModel->where('id','IN',array_unique($coupon_ids))->field('id,name')->select();
$shopInfo ? $shopInfo = $shopInfo->toArray() : [];//门店信息
$shop = array_column($shopInfo,"name","id");
foreach($user_coupon as $k=>$v){
$back_result[$k]['id'] = $v['id'];
$back_result[$k]['coupon_id'] = $v['coupon']['id'];
$back_result[$k]['name'] = $shop[$v['coupon']['shop_id']];
$back_result[$k]['start_time'] = $v['coupon']['start_time'];
$back_result[$k]['stop_time'] = $v['coupon']['stop_time'];
$back_result[$k]['price'] = $v['coupon']['price'];
$back_result[$k]['coupon_status'] = $v['coupon']['status'];
}
}
return [
'page' => $page,
'pages' => ceil($total / $pageSize),
'total' => $total,
'list' => $back_result,
];
}
{
"code": 0,
"msg": "成功",
"time": 1609514782,
"data": {
"page": "1",
"pages": 1,
"total": 7,
"list": [
{
"id": 7,
"coupon_id": 7,
"name": "五号店小龙虾",
"start_time": 1609423368,
"stop_time": 1611072000,
"price": "100.0000",
"coupon_status": 0
},
{
"id": 6,
"coupon_id": 6,
"name": "五号店小龙虾",
"start_time": 1609423082,
"stop_time": 1611072000,
"price": "800.0000",
"coupon_status": 0
},
{
"id": 5,
"coupon_id": 5,
"name": "五号店小龙虾",
"start_time": 1609422832,
"stop_time": 1611072000,
"price": "100.0000",
"coupon_status": 0
},
{
"id": 4,
"coupon_id": 4,
"name": "五号店小龙虾",
"start_time": 1609422632,
"stop_time": 1611072000,
"price": "10.0000",
"coupon_status": 0
},
{
"id": 3,
"coupon_id": 3,
"name": "五号店小龙虾",
"start_time": 1609422604,
"stop_time": 1611072000,
"price": "60.0000",
"coupon_status": 0
},
{
"id": 2,
"coupon_id": 2,
"name": "五号店小龙虾",
"start_time": 1609418756,
"stop_time": 1609424634,
"price": "30.0000",
"coupon_status": 0
},
{
"id": 1,
"coupon_id": 1,
"name": "五号店小龙虾",
"start_time": 1609418715,
"stop_time": 1609424634,
"price": "50.0000",
"coupon_status": 0
}
]
}
}
转载注明:
感谢博主,喝杯咖啡~
感谢博主,喝杯咖啡~
还没有人发表评论