如何在mysql中按组排序?

时间:2017-04-21 07:36:56

标签: mysql sql database

我有下面的代码,但我想通过date_available desc安排它并将其分组为store_id,这样我只会返回1个值。

 SELECT
product_to_store.store_id,
product.date_available,
product.product_id,
product_description.name,
product_to_category.category_id, 
pcd3.name,
lvl2.parent_id,
pcd2.name,
CASE
WHEN lvl2.parent_id > 0 THEN lvl1.parent_id
ELSE 0
END,

pcd1.name,
product.quantity,
product_type.type

FROM product 
inner join product_to_store on (product.product_id=product_to_store.product_id)
inner join product_description on (product.product_id = product_description.product_id)
inner join product_to_category on (product.product_id = product_to_category.product_id)
inner join product_type on (product.product_id = product_type.product_id)
inner join store on  (product_to_store.store_id = store.store_id)
inner join product_category as lvl2 on (product_to_category.category_id = lvl2.category_id)
inner join product_category as lvl1 on (lvl1.category_id = lvl2.parent_id)
inner join product_category_description as pcd3 on (pcd3.category_id = lvl2.category_id)
inner join product_category_description as pcd2 on (pcd2.category_id = lvl2.parent_id)
inner join product_category_description as pcd1 on (pcd1.category_id = lvl1.parent_id)

样本表:

store_id    date_available  name
1            2017-05-04     T1
1          0000-00-00       T2
2          2017-06-04       T3
3         0000-00-00        T4
2         2017-04-04        T5
3         0000-00-00        T6

预期结果:

store_id    date_available  name
1              2017-05-04   T1
2              2017-06-04   T3
3             0000-00-00    T4

我怎么能在mysql中做到这一点?请帮帮我谢谢。

1 个答案:

答案 0 :(得分:0)

使用此提示:

-- Create some testing data
IF OBJECT_ID('tempdb..#product') IS NOT NULL DROP TABLE #product 
CREATE TABLE #product(
    [store_id] int
    ,[date_available] varchar(50)   
    ,[name] varchar(50)
)
INSERT INTO #product VALUES 
    (1, '2017-05-04', 'T1')
   ,(1, '0000-00-00', 'T2')
   ,(2, '2017-06-04', 'T3')
   ,(3, '0000-00-00', 'T4')
   ,(2, '2017-04-04', 'T5')
   ,(3, '0000-00-00', 'T6')

-- Get result
SELECT p1.[store_id], p1.[date_available], MIN(p2.[name]) AS [name]
    FROM (
        SELECT [store_id], MAX([date_available]) AS [date_available] FROM #product
            GROUP BY [store_id]
    ) p1
    INNER JOIN #product p2
        ON p1.[store_id] = p2.[store_id] AND p1.[date_available] = p2.[date_available]
    GROUP BY p1.[store_id], p1.[date_available]

-- Destroy the temp table
DROP TABLE #product 

然后在您的长脚本中,您可以将其重写为以下内容:

SELECT product_to_store.store_id,
    product.date_available,
    product.product_id,
    .
    .
    .
FROM product
INNER JOIN ...
INNER JOIN ...
INNER JOIN (
    SELECT p1.[store_id], p1.[date_available], MIN(p2.[name]) AS [name]
        FROM (
            SELECT [store_id], MAX([date_available]) AS [date_available] FROM #product
                GROUP BY [store_id]
        ) p1
        INNER JOIN #product p2
            ON p1.[store_id] = p2.[store_id] AND p1.[date_available] = p2.[date_available]
        GROUP BY p1.[store_id], p1.[date_available]
) store ON (product_to_store.store_id = store.store_id)
INNER JOIN ...
INNER JOIN ...