我的 SQL 脚本没问题,但我无法在 Laravel 6 中编写此 SQL 脚本
My SQL script is okay but I can't write this SQL script in Laravel 6
SELECT appointmentDate
, appiontmentTime
, (SELECT GROUP_CONCAT(name) name
FROM users ta
JOIN appointment u
ON u.salesperson = ta.id
WHERE u.appiontmentTime = appt.appiontmentTime
AND u.appointmentDate = appt.appointmentDate
) usersname
FROM appointment appt
WHERE appointmentDate BETWEEN "2020-05-01" AND "2020-05-31"
GROUP
BY appointmentDate
, appiontmentTime
我看不出有什么理由需要这里的子查询。我认为以下查询应该 return 相同的结果:
SELECT a.appointmentDate
, a.appiontmentTime
, GROUP_CONCAT(u.name) name
FROM appointment a
LEFT JOIN users u ON u.id = a.salesperson
WHERE a.appointmentDate BETWEEN "2020-05-01" AND "2020-05-31"
GROUP
BY a.appointmentDate
, a.appiontmentTime
使用 laravels 查询生成器它将是:
$data = DB::table('appointment as a')
->leftJoin('users as u', 'u.id', '=', 'a.salesperson')
->whereBetween('a.appointmentDate', ['2020-05-01', '2020-05-31'])
->groupBy('a.appointmentDate', 'a.appiontmentTime')
->select('a.appointmentDate', 'a.appiontmentTime')
->selectRaw('GROUP_CONCAT(u.name) as name')
->get();
SELECT appointmentDate
, appiontmentTime
, (SELECT GROUP_CONCAT(name) name
FROM users ta
JOIN appointment u
ON u.salesperson = ta.id
WHERE u.appiontmentTime = appt.appiontmentTime
AND u.appointmentDate = appt.appointmentDate
) usersname
FROM appointment appt
WHERE appointmentDate BETWEEN "2020-05-01" AND "2020-05-31"
GROUP
BY appointmentDate
, appiontmentTime
我看不出有什么理由需要这里的子查询。我认为以下查询应该 return 相同的结果:
SELECT a.appointmentDate
, a.appiontmentTime
, GROUP_CONCAT(u.name) name
FROM appointment a
LEFT JOIN users u ON u.id = a.salesperson
WHERE a.appointmentDate BETWEEN "2020-05-01" AND "2020-05-31"
GROUP
BY a.appointmentDate
, a.appiontmentTime
使用 laravels 查询生成器它将是:
$data = DB::table('appointment as a')
->leftJoin('users as u', 'u.id', '=', 'a.salesperson')
->whereBetween('a.appointmentDate', ['2020-05-01', '2020-05-31'])
->groupBy('a.appointmentDate', 'a.appiontmentTime')
->select('a.appointmentDate', 'a.appiontmentTime')
->selectRaw('GROUP_CONCAT(u.name) as name')
->get();