我有這樣的查詢,它給我關於暢銷書商品的結果,目前它可以正常工作,但現在我只想從每個商店獲得一個產品,以便擁有不同的si
。對查詢計數不同的行的MySQL查詢
+--------+---------+-------+
| id | shop_id | count |
+--------+---------+-------+
| 425030 | 38027 | 111 |
| 291974 | 5368 | 20 |
| 425033 | 38027 | 18 |
| 291975 | 5368 | 12 |
| 142776 | 5368 | 10 |
| 397016 | 38027 | 9 |
| 291881 | 5368 | 8 |
+--------+---------+-------+
任何想法:從商店
SELECT `si`.`id`, si.shop_id,
(SELECT COUNT(*)
FROM `transaction_item` AS `tis`
JOIN `transaction` as `t`
ON `t`.`id` = `tis`.`transaction_id`
WHERE `tis`.`shop_item_id` = `si`.`id`
AND `t`.`added_date` >= '2014-02-26 00:00:00')
AS `count`
FROM `shop_item` AS `si`
INNER JOIN `transaction_item` AS `ti`
ON ti.shop_item_id = si.id
GROUP BY `si`.`id`
ORDER BY `count` DESC LIMIT 7
這給了像畝的結果shop_id
只有一個暢銷的產品?
編輯 所以我創建了一個搗鼓它 http://sqlfiddle.com/#!2/cfc4c/1
現在查詢返回賣得最好的,我希望它從shopso小提琴的結果只返回一個產品的產品應該是
+----+---------+-------+
| ID | SHOP_ID | COUNT |
+----+---------+-------+
| 1 | 222 | 3 |
| 4 | 333 | 2 |
| 8 | 555 | 1 |
| 9 | 777 | 1 |
+----+---------+-------+
請提供一些示例數據和期望的結果。你怎麼知道交易中的物品? –