gpt4 book ai didi

php - MYSQL/Query Builder/Eloquent - 将行转列

转载 作者:行者123 更新时间:2023-11-29 03:33:15 27 4
gpt4 key购买 nike

** 排程系统(按周预约的数量):**

我希望生成一个仪表板,显示顾问列表以及他们在未来几周安排的预约数量。

所以我的 sql 查询/针对我的特定场景的 eloquent 等效查询如下:

select 
consultant_id,
count(appointment) as num_appointments,
YEARWEEK(appointment, 1) as year_week
from
appointments
where
appointments.deleted_at is null
and appointments.consultant_id in (25, 29, 19, 38, 37, 32, 14, 21, 12, 40)
and appointment > '2014-12-07 16:39:07'
and YEARWEEK(now(), 1) + 4 >= YEARWEEK(appointment, 1)
group by consultant_id, YEARWEEK(appointment, 1)
order by consultant_id, YEARWEEK(appointment, 1) asc;


$consultants = $this->consultant
->with(['appointments' => function($q) {
$q->scheduled()
->where(\DB::raw('YEARWEEK(now(), 1) + 4'), '>=', \DB::raw('YEARWEEK(appointment, 1)'))
->select([
'consultant_id',
\DB::raw('count(appointment) as num_appointments'),
\DB::raw('YEARWEEK(appointment, 1) as year_week'),
])
->groupBy(\DB::raw('consultant_id, YEARWEEK(appointment, 1)'))
->orderBy(\DB::raw('consultant_id, YEARWEEK(appointment, 1)', 'DESC'));
}])
->orderBy('name')
->paginate(10);

这对每个顾问都是有效的,如果有预约,它会提供如下信息:

consultant_id   num_appointments    year_week
14 5 201450
14 3 201451
29 4 201450
29 1 201451
40 1 201450
40 1 201452

但是,如果没有约会,year_week 将丢失。

所以在 mysql 中然后在 Laravel 的查询构建器中,我希望能够将 year_week 行转置为列。这样表格就会输出:

consultant_id    week0     week1    week2
consultant_id 201450 201451 201452
14 5 3 NULL
29 4 1 NULL
40 1 NULL 1

更新

SELECT
consultant_id,
COUNT(CASE WHEN YEARWEEK(DATE_ADD(now(), INTERVAL 0 WEEK), 1) = YEARWEEK(appointment, 1) THEN 1 END) as this_week,
COUNT(CASE WHEN YEARWEEK(DATE_ADD(now(), INTERVAL 1 WEEK), 1) = YEARWEEK(appointmnet, 1) THEN 1 END) as week1,
COUNT(CASE WHEN YEARWEEK(DATE_ADD(now(), INTERVAL 2 WEEK), 1) = YEARWEEK(appointmnet, 1) THEN 1 END) as week2
FROM
demand as d
WHERE date_created >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH)
GROUP BY consultant_id;

最佳答案

解决方案 1:在 spatie macro collection 中使用转置

转置的目标是旋转一个多维数组,将行变成列,将列变成行。

collect([
['Jane', 'Bob', 'Mary'],
['jane@example.com', 'bob@example.com', 'mary@example.com'],
['Doctor', 'Plumber', 'Dentist'],
]
)->transpose()->toArray();

// [
// ['Jane', 'jane@example.com', 'Doctor'],
// ['Bob', 'bob@example.com', 'Plumber'],
// ['Mary', 'mary@example.com', 'Dentist'],
// ]

解决方案 2:使用 group by in collectiongroupBy 方法按给定键对集合的项目进行分组:

$collection = collect([
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
['account_id' => 'account-x11', 'product' => 'Desk'],
]);

$grouped = $collection->groupBy('account_id');

$grouped->toArray();

/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/

关于php - MYSQL/Query Builder/Eloquent - 将行转列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348304/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com