我读了很多关于这一点,但没有为我工作。 有人能帮忙吗?
我有很多不同的文章(很多有相同EAN)一张大桌子和需要永远只有最便宜的一款与纠正(按价格排序):
*art price an ean
*Test |79,00|15770|0808736558136
*Test |85,00|k3238|0808736558136
*Test |68,00|r4850|0808736558136
*Test |65,00|a1117|0808736558136
*Test |78,00|t8619|0808736558136
想到这一个:
*Test |65,00|a1117|0808736558136
SELECT B.*
FROM BigTable AS B -- Why do SQL questions omit the table names so often?
JOIN (SELECT EAN, MIN(Price) AS Price
FROM BigTable
GROUP BY EAN
) AS P
ON B.EAN = P.EAN AND B.Price = P.Price
ORDER BY B.EAN;
子查询查找每个EAN的最低价格; 外部查询发现符合该EAN EAN和最低价格的细节。 如果有两个记录有相同的最低价格定EAN,都将被选择。
SELECT * FROM myTable ORDER BY price ASC LIMIT 1
文章来源: Find details for minimum price entry for each group of rows with the same article number