Mysql - 为符合条件的记录选择语句,分组依据

时间:2016-07-13 13:40:17

标签: mysql sql

我有下表' StockItems' (本例简化):

+----+----------+---------+---------+---------+-----+
| ID | SubCatID |  Item   | GroupID |  Size   | Def |
+----+----------+---------+---------+---------+-----+
|  1 |        1 |    40mm |       2 |    1    |   1 |
|  2 |        1 |    40mm |       2 |    1    |   0 |
|  3 |        1 |    75mm |       3 |    1    |   1 |
|  4 |        2 |    90mm |       4 |    NULL |   1 |
+----+----------+---------+---------+---------+-----+

以下是我的查询:

SELECT ID, SubCatID, Item, GroupID 
FROM StockItems 
WHERE ((GroupID = 0 OR GroupID IS NULL) OR ((GroupID IS NOT NULL OR GroupID <> 0) AND Def = 1) OR Size IS NOT NULL) AND Del=0 
ORDER BY SubCatID, Size, Item+0, Item

其中Def是GroupID中的Default项,Del代表Deleted

这有效,但我还需要一步才能达到我想要的结果:我不想检索具有相同GroupID和大小WHERE SubCatID = 1的记录

如果任何groupID的GroupID和Size相同,我希望它检索Def = 1的记录

我几乎使用以下查询到达那里:

SELECT ID, SubCatID, Item, GroupID 
FROM StockItems 
WHERE ((GroupID = 0 OR GroupID is Null) OR ((GroupID IS NOT NULL OR GroupID <> 0) AND Def = 1) OR Size IS NOT NULL) AND Del=0 
GROUP BY GroupID, Size 
ORDER BY SubCatID, Size, Item+0, Item

但我只想将GROUP BY应用于SubCatID = 1

的记录

我真的不确定GROUP BY是否可以到达这里,它是我能找到的最接近的人

第一个查询的输出:

+-----+----------+---------------+----------+
| ID  | SubCatID |     Item      | GroupID  |
+-----+----------+---------------+----------+
|   1 |        1 |    40mm Pipe  |  2       |
| 331 |        1 |    40mm Pipe  | 2        |
| 329 |        1 |    63mm Pipe  | 3        |
| 330 |        1 |    75mm Pipe  | 4        |
|   2 |        1 |    40mm Pipe  | 2        |
| 332 |        1 |    40mm Pipe  | 2        |
|   3 |        1 |    63mm Pipe  | 3        |
|   4 |        1 |    75mm Pipe  | 4        |
|   6 |        1 |    110mm Pipe | 6        |
|   7 |        1 |    125mm Pipe | 7        |
|   8 |        1 |    160mm Pipe | 8        |
|   9 |        1 |    200mm Pipe | 9        |
|  10 |        1 |    250mm Pipe | 10       |
|  11 |        1 |    315mm Pipe | 11       |
|  12 |        1 |    355mm Pipe | 12       |
|  13 |        1 |    400mm Pipe | 13       |
|  14 |        1 |    450mm Pipe | 14       |
|   5 |        1 |    90mm Pipe  |  5       |
|  15 |       18 |   63mm 90 Be  | NULL     |
|  16 |       18 |   75mm 90 Be  | NULL     |
|  17 |       18 |   90mm 90 Be  | NULL     |
|  18 |       18 |   110mm 90 Be | NULL     |
|  19 |       18 |   125mm 90 Be | NULL     |
|  20 |       18 |   160mm 90 Be | NULL     |
+-----+----------+---------------+----------+

后一个查询的输出:

+-----+----------+---------------+----------+
| ID  | SubCatID |     Item      | GroupID  |
+-----+----------+---------------+----------+
|   1 |        1 |    40mm Pipe  | 2        |
| 329 |        1 |    63mm Pipe  | 3        |
| 330 |        1 |   75mm Pipe   | 4        |
|   2 |        1 |    40mm Pipe  | 2        |
|   3 |        1 |    63mm Pipe  | 3        |
|   4 |        1 |    75mm Pipe  | 4        |
|   6 |        1 |    110mm Pipe | 6        |
|   7 |        1 |    125mm Pipe | 7        |
|   8 |        1 |    160mm Pipe | 8        |
|   9 |        1 |    200mm Pipe | 9        |
|  10 |        1 |    250mm Pipe | 10       |
|  11 |        1 |    315mm Pipe | 11       |
|  12 |        1 |    355mm Pipe | 12       |
|  13 |        1 |    400mm Pipe | 13       |
|  14 |        1 |    450mm Pipe | 14       |
|   5 |        1 |    90mm Pipe  |  5       |
|  15 |       18 |   63mm 9B     | NULL     |
| 327 |       61 |  Stainless St | 20       |
+-----+----------+---------------+----------+

这就是我想要的,但是有很多记录,其中GroupID = Null,Size = Null。因此,这些不会出现在结果中。所以我只希望GROUP BY出现在SubCatID = 1

的地方

2 个答案:

答案 0 :(得分:0)

你需要你使用HAVING。所以有如下内容:

SELECT ID, SubCatID, Item, GroupID 
FROM StockItems
GROUP BY GroupID
HAVING SubCatID = 1

MySQL Having

答案 1 :(得分:0)

以下是我最终使用的解决方案:

SELECT ID, SubCatID, Item, GroupID, Size
FROM StockItems 
WHERE (((GroupID IS NOT NULL OR GroupID <> 0) AND Def = 1) OR Size IS NOT NULL) AND Del=0 
GROUP BY GroupID, Size 
UNION ALL

    SELECT ID, SubCatID, Item, GroupID, Size
    FROM StockItems
    WHERE (GroupID = 0 OR GroupID is Null)
ORDER BY SubCatID, Size, Item+0, Item