MYSQL 结合排序依据和分组依据
MYSQL combine order by with group by
我需要有关 mysql 查询的帮助。
我经历了之前所有关于 ORDER BY 和 GROUP BY 组合的讨论,我理解分组在排序之前执行的概念,因此排序不会按预期工作。
这表示由于我在 sql 方面的经验有限,我无法设法使我的查询适应使用所提供的任何示例。
有更多经验的人可以指出我正确的方向吗?
我有一个链接到数据馈送 table 的产品集合。
我需要按关键字搜索产品 table,并为每个数据源搜索 return 最便宜的产品。
有可能有多种产品具有相同的低价,我需要 return 每个数据源只需要一个。
这是我到目前为止的情况:
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
这 return 每个数据馈送一个产品,但显然不是最便宜的,因为分组是在排序之前执行的
非常感谢您的帮助!
我没有确切的 table 和数据来测试我的查询,但根据您的要求,我认为如果您这样尝试,应该会提供所需的输出。
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
经过一些试验,我找到了一种使用 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
我需要有关 mysql 查询的帮助。 我经历了之前所有关于 ORDER BY 和 GROUP BY 组合的讨论,我理解分组在排序之前执行的概念,因此排序不会按预期工作。 这表示由于我在 sql 方面的经验有限,我无法设法使我的查询适应使用所提供的任何示例。 有更多经验的人可以指出我正确的方向吗? 我有一个链接到数据馈送 table 的产品集合。 我需要按关键字搜索产品 table,并为每个数据源搜索 return 最便宜的产品。 有可能有多种产品具有相同的低价,我需要 return 每个数据源只需要一个。
这是我到目前为止的情况:
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
这 return 每个数据馈送一个产品,但显然不是最便宜的,因为分组是在排序之前执行的
非常感谢您的帮助!
我没有确切的 table 和数据来测试我的查询,但根据您的要求,我认为如果您这样尝试,应该会提供所需的输出。
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
经过一些试验,我找到了一种使用 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