0

我有一个这样的产品表

+----------+------+-----------------+-------+-------+
| ParentID | SKU  |       Name      | Price | Color |
+----------+------+-----------------+-------+-------+
|        1 |  qwe | Product 1 Red   |     12| Reb   |
|        1 |  qaz | Product 1 Blue  |     13| Blue  |
|        1 |  erf | Product 1 Green |     11| Green |
|        2 |  tgb | Product 2 Black |     12| Black |
|        2 |  yhj | Product 2 White |     12| White |
|        3 |  ujk | Product 3 Red   |     15| Red   |
|        3 |  kjm | Product 3 Blue  |     19| Blue  |
|        3 |  gfd | Product 3 Pink  |     17| Pink  |
|        3 |  vgy | Product 3 White |     16| White |
|        3 |  njk | Product 3 Black |     10| Black |
|        4 |  cfg | Product 4 Red   |     12| Red   |
+----------+------+-----------------+-------+-------+

我想要的是一个查询,以获取每个包含所有项目信息的 parentid 的项目。我不在乎哪一行,但它应该包含该行的所有信息,并且每个 parentid 只有一个

我需要留下这样的东西

+----------+------+-----------------+-------+-------+
| ParentID | SKU  |       Name      | Price | Color |
+----------+------+-----------------+-------+-------+
|        1 |  qaz | Product 1 Blue  |     13| Blue  |
|        2 |  yhj | Product 2 White |     12| White |
|        3 |  gfd | Product 3 Pink  |     17| Pink  |
|        4 |  cfg | Product 4 Red   |     12| Red   |
+----------+------+-----------------+-------+-------+
4

1 回答 1

0

你可以试试:

Select t.* from table t, 
(select parentId, min(price) as price
from table
group by parentId) a
where t.parentId = a.parentId
and t.price = a.price

更新:

SET @rownum := 0;

select min(b.rank), b.parentID, SKU,Name ,Price, Color from (
select @rownum := @rownum + 1 AS rank, parentID, SKU,Name ,Price, Color
from t1) b
group by b.parentID

链接到小提琴

于 2013-10-02T17:45:52.107 回答