我一直在尋找一種方法來適應找到最大和最小的多個結果。我發現這個鏈接上一個問題: max Countsmysql max有多個實例
答案之一是給出:
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
)
這是公認的做法尤其是對於大型數據庫?如果這有意義,上述查詢是否基本上在本身內部運行?
我有一個更復雜的查詢也需要找到馬和最小。我想優化它:
編輯:
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
我試圖想不同的方式來找到max和沒有運氣閔至今。任何更多的幫助,將不勝感激 謝謝 mc
任何幫助嗎? – mrcurious