我正在寻找一种方法来适应有多个结果来找到最大值和最小值。我找到了上一个问题的链接: max Counts
给出的答案之一是:
SELECT color_id, COUNT(color_id) totalCount
FROM products
WHERE item_id = 1234
GROUP BY color_id
HAVING COUNT(color_id) =
(
SELECT COUNT(color_id) totalCount
FROM products
WHERE item_id = 1234
GROUP BY color_id
ORDER BY totalCount DESC
LIMIT 1
)
这种公认的做法尤其适用于大型数据库吗?如果有意义的话,上面的查询不是基本上在自身内部运行吗?
我有一个更复杂的查询,还需要找到 ma 和 min。我想优化它:
编辑:
SELECT `system_users`.`first`, `system_users`.`last`, COUNT(`quotes`.`created_by`) as most_quotes
FROM `quotes`
INNER JOIN `system_users`
ON `quotes`.`created_by` = `system_users`.`id`
where `system_users`.`store_id` = '$createdID'
and `quotes`.`date_created` between '$startDate' and '$endDate' group by(`created_by`)
HAVING count(`quotes`.`created_by`) =
(
SELECT COUNT(`quotes`.`created_by`)
FROM `quotes`
INNER JOIN `system_users`
ON `quotes`.`created_by` = `system_users`.`id`
where `system_users`.`store_id` = '$createdID'
and `quotes`.`date_created` between '$startDate' and '$endDate' group by(`created_by`) ORDER BY count(`created_by`) DESC limit 1
)
OR
(
SELECT COUNT(`quotes`.`created_by`)
FROM `quotes`
INNER JOIN `system_users`
ON `quotes`.`created_by` = `system_users`.`id`
where `system_users`.`store_id` = '$createdID'
and `quotes`.`date_created` between '$startDate' and '$endDate' group by(`created_by`) ORDER BY count(`created_by`) ASC limit 1
)
ORDER BY most_quotes ASC
到目前为止,我正在尝试寻找不同的方法来找到最大值和最小值。对此的任何更多帮助将不胜感激谢谢mc