SQL Select only rows with Minimum Value on a Column with Where Condition

后端 未结 2 1801

Table:

| id | productId | orderIndex | rejected |
------------------------------------------
| 1  |  1        |   0        |   1      |
| 2  |  1        |   1             


        
2条回答
  •  一整个雨季
    2021-01-12 02:53

    http://sqlfiddle.com/#!9/0196f/2

    SELECT DISTINCT t.*
    FROM table1 t
    INNER JOIN (
    SELECT productId, min(orderIndex) minIdx
    FROM table1
    WHERE rejected = 0
    GROUP BY productId
      ) t1
    ON t.productId = t1.productId
      AND t.orderIndex = t1.minIdx;
    

提交回复
热议问题