我对此查询有点困难。 GROUP BY函数在此查询中不起作用。没有网店集团的工作,但多个网上商店显示。也许有人有个主意?谢谢!
SELECT
default_deals.id,
default_deals.title,
default_webshops.name,
COUNT(default_deals_categories.categories_id) as category_count,
default_webshops.popular,
default_webshops.popular_order
FROM
default_deals
JOIN default_deals_categories ON default_deals.id = default_deals_categories.row_id
JOIN default_webshops ON default_deals.webshop = default_webshops.id
WHERE
categories_id IN (1, 2)
GROUP BY
default_deals.id,
default_deals.title,
default_webshops.name,
default_webshops.popular,
default_webshops.popular_order
ORDER BY
category_count DESC,
default_webshops.popular DESC,
default_webshops.popular_order DESC
答案 0 :(得分:0)
试试这个:
SELECT
default_deals.id,
title,
name,
COUNT(categories_id) as category_count,
popular,
popular_order
FROM
default_deals
JOIN default_deals_categories ON default_deals.id = default_deals_categories.row_id
JOIN default_webshops ON default_deals.webshop = default_webshops.id
WHERE
categories_id IN (1, 2)
GROUP BY
default_deals.id,
title,
name,
popular,
popular_order
ORDER BY
category_count DESC,
popular DESC,
popular_order DESC
好吧,我猜你的搜索结果已经多次列出了它已被链接到的每个网上商店然后相同的类别数量,但是流行和热门订单的价值可能不同?您需要做的是决定如何聚合这些字段。例如,您可以这样做:
SELECT
dd.id,
dd.title,
MAX(dw.name) AS webshop_name,
COUNT(dc.categories_id) AS category_count,
MAX(dw.popular) AS popular,
SUM(dw.popular_order) AS popular_order
FROM
default_deals dd
JOIN default_deals_categories dc ON dd.id = dc.row_id
JOIN default_webshops dw ON dd.webshop = dw.id
WHERE
dc.categories_id IN (1, 2)
GROUP BY
dd.id,
dd.title
ORDER BY
COUNT(dc.categories_id) DESC,
MAX(dw.popular) DESC,
SUM(dw.popular_order) DESC;
但我敢打赌,这不是你真正想要看到的。你有多个网上商店,你想看到什么?我的例子只是按字母顺序取最高值,但你可以把它变成一个列表,例如" Webshop1,Webshop2"等
您还需要决定如何聚合webshop表中的其他字段。我的查询取最受欢迎的和popular_order的总和,但这只是一个例子。
最后,这会增加一倍,增加三倍等等。这取决于一笔交易与多少网上商店挂钩......我打赌这也不是你想要的?