【发布时间】:2022-01-07 10:23:52
【问题描述】:
我在 laravel 中使用多个条件和不同条件的连接,但我有一个问题,数量和日期列始终为空,但是当我在数据库中看到该列不为空时,它有一些值,当我只为空时我正在使用连接。
$contact_packages = Contact::where('contacts.id','!=',1)
->leftJoin('transactions as t','t.id','=','contacts.id')
->leftJoin('transaction_sell_lines as tsl','tsl.transaction_id','=','t.id')
->join('customer_package as cp','contacts.id','=','cp.contact_id')
->join('package as p','cp.package_id','=','p.id')
->join('customer_package_service as cps','cps.customer_package_id','=','cp.id')
->leftJoin('categories as c','p.category_id','=','c.id')
->leftJoin('categories as sc','p.sub_category_id','=','sc.id')
// ->where('cps.customer_package_id','cp.id')
->select([
'contacts.id as cid',
'contacts.name as cname',
'p.id as pid',
'p.name as pname',
DB::raw('TRIM(tsl.quantity)+0 as pqty'),
// 'tsl.quantity as pqty',
DB::raw('count(cps.product_id) as services'),
DB::raw('COUNT(CASE WHEN cps.status = 0 THEN cps.product_id END) as pending'),
DB::raw('COUNT(CASE WHEN cps.status = 1 THEN cps.product_id END) as redeemed'),
't.created_at as date',
'c.name as pc',
'sc.name as psc',
'p.expire_days as ped',
'cps.customer_package_id as cpid'
])
->groupBy('cpid')
->get();
select `contacts`.`id` as `cid`, `contacts`.`name` as `cname`,
`p`.`id` as `pid`, `p`.`name` as `pname`,
TRIM(tsl.quantity)+0 as pqty, count(cps.product_id) as services,
COUNT(CASE WHEN cps.status = 0 THEN cps.product_id END) as pending,
COUNT(CASE WHEN cps.status = 1 THEN cps.product_id END) as redeemed,
`t`.`created_at` as `date`, `c`.`name` as `pc`,
`sc`.`name` as `psc`, `p`.`expire_days` as `ped`,
`cps`.`customer_package_id` as `cpid` from `contacts`
left join `transactions` as `t` on `t`.`id` = `contacts`.`id`
left join `transaction_sell_lines` as `tsl` on `tsl`.`transaction_id` = `t`.`id`
inner join `customer_package` as `cp` on `contacts`.`id` = `cp`.`contact_id`
inner join `package` as `p` on `cp`.`package_id` = `p`.`id`
inner join `customer_package_service` as `cps` on `cps`.`customer_package_id` = `cp`.`id`
left join `categories` as `c` on `p`.`category_id` = `c`.`id`
left join `categories` as `sc` on `p`.`sub_category_id` = `sc`.`id`
where `contacts`.`id` != ?
and `contacts`.`deleted_at` is null
group by `cpid`
【问题讨论】:
-
哪一个?或两者?
`t.created_at`或`p.expire_days` -
t.created_at 和 tsl.quantity
-
这两个字段的数据类型是什么
-
`t.created_at 是时间戳,tsl.quantity 是整数
-
你为什么要修剪一个整数