我一直试图想出这个问题几个小时,但我无法理解为什么它不起作用。
我有一个MySQL查询,如下所示:
SELECT
*,
COUNT(Schedule.id) AS schedule_count,
GetDistance(49.0134297, 12.1016236, Provider.latitude, Provider.longitude) AS distance,
Provider.gps_modified >= DATE_SUB(NOW(), INTERVAL 180 MINUTE) AS valid_gps,
`Provider`.`id`
FROM `db`.`providers` AS `Provider`
LEFT JOIN `availapple`.`employments` AS `Employment` ON (`Provider`.`employment_id` = `Employment`.`id`)
INNER JOIN `db`.`certificates_providers` AS `CertProv` ON (`CertProv`.`provider_id` = `Provider`.`id`)
INNER JOIN `db`.`schedules` AS `Schedule` ON (`Schedule`.`provider_id` =
`Provider`.`id`)
WHERE `Provider`.`id` IN (1, 19, 23, 28, 48, 63, 96, 97, 126, 128, 150, 157, 172)
AND `Provider`.`last_login` >= DATE_SUB(NOW(), INTERVAL 180000 MINUTE)
ORDER BY `Provider`.`activity_points` DESC, `schedule_count` DESC
LIMIT 10
有10个提供程序符合WHERE子句中指定的条件,但是我只得到一个结果。
如果我摆脱COUNT(Schedule.id) AS schedule_count
但它会返回所有10个结果。
答案 0 :(得分:2)
您应该按Provider
添加该群组。id
这样
SELECT
*,
COUNT(Schedule.id) AS schedule_count,
GetDistance(49.0134297, 12.1016236, Provider.latitude, Provider.longitude) AS distance,
Provider.gps_modified >= DATE_SUB(NOW(), INTERVAL 180 MINUTE) AS valid_gps,
`Provider`.`id`
FROM `db`.`providers` AS `Provider`
LEFT JOIN `availapple`.`employments` AS `Employment` ON (`Provider`.`employment_id` = `Employment`.`id`)
INNER JOIN `db`.`certificates_providers` AS `CertProv` ON (`CertProv`.`provider_id` = `Provider`.`id`)
INNER JOIN `db`.`schedules` AS `Schedule` ON (`Schedule`.`provider_id` =
`Provider`.`id`)
WHERE `Provider`.`id` IN (1, 19, 23, 28, 48, 63, 96, 97, 126, 128, 150, 157, 172)
AND `Provider`.`last_login` >= DATE_SUB(NOW(), INTERVAL 180000 MINUTE)
group by `Provider`.`id`
ORDER BY `Provider`.`activity_points` DESC, `schedule_count` DESC
LIMIT 10