我需要有关mysql查询的帮助。 我经历了之前有关ORDER BY和GROUP BY合并的所有讨论,并且我了解了在排序之前执行分组的概念,因此排序将无法按预期进行。 这表示由于我在sql方面的经验有限,因此我无法设法使查询适合使用提供的任何示例。 可以给有更多经验的人指出正确的方向吗? 我有一个链接到数据馈送表的产品集合。 我需要按关键字搜索产品表,并为每个数据Feed返回最便宜的产品。 可能有多个产品的价格都一样低,而每个数据Feed我只需要退货一次。
这是我到目前为止所拥有的:
select p.*, m.*, d.* from datafeeds as d,
(
SELECT *
FROM products AS p
WHERE 1=1
AND (match(p.name) against ('+saucony +"ride iso 2" -women' in boolean mode))
AND p.is_custom = 0
ORDER BY merchant_name,
display_price
) AS p
LEFT OUTER JOIN meta as m on p.mykey = m.mykey
WHERE p.datafeed_id = d.id
AND (match(p.name) against ('+saucony +"ride iso 2" -women' in boolean mode))
AND p.is_custom = 0
GROUP BY d.merchant_name
ORDER BY p.display_price asc limit 50
这会为每个数据Feed返回一个产品,但显然不是最便宜的,因为分组是在排序之前执行的
非常感谢您的帮助!
答案 0 :(得分:0)
经过一些实验,我找到了一种使用INNER JOIN的方法
我将其发布为答案,以防万一有人需要
SELECT p.*
FROM products AS p
INNER JOIN
(
SELECT p.datafeed_id,
Min(p.display_price) AS minprice
FROM products AS p,
datafeeds AS d
WHERE p.datafeed_id = d.id
AND (MATCH(p.name) against ('+saucony +"ride iso 2"' IN boolean mode))
AND p.is_custom = 0
GROUP BY p.datafeed_id ) AS p2
ON (
p.display_price = p2.minprice
AND p.datafeed_id = p2.datafeed_id)
WHERE (
MATCH(p.name) against ('+saucony +"ride iso 2"' IN boolean mode))
AND p.is_custom = 0
GROUP BY p.datafeed_id
LIMIT 50
答案 1 :(得分:-1)
我没有确切的表和数据来测试我的查询,但是根据您的要求,我认为,如果您这样尝试,则应提供所需的输出。
select p.*, m.*, d.* from datafeeds as d, (
SELECT *,min(productPrice) productPrice
FROM products AS p
WHERE 1=1
and (match(p.name) against ('+saucony +"ride iso 2" -women' in boolean mode)) and p.is_custom = 0
group by p.name,min(productPrice)
ORDER BY merchant_name,
display_price) as p
left outer join meta as m on p.mykey = m.mykey where p.datafeed_id = d.id and (match(p.name) against ('+saucony +"ride iso 2" -women' in boolean mode)) and p.is_custom = 0
group by d.merchant_name,productPrice
order by p.display_price asc limit 50