简体   繁体   中英

Mysql - Select statement, Group By for records which match criteria

I have the following table 'StockItems' (Simplified for this example):

+----+----------+---------+---------+---------+-----+
| 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 |
+----+----------+---------+---------+---------+-----+

Here is the query I have:

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

Where Def is the Default item in the GroupID, and Del stands for Deleted

This works but I need 1 more step to achieve the results I want: I dont want to retrieve records with the same GroupID and Size WHERE SubCatID = 1

If the GroupID and Size are the same for any groupID I want it to retrieve the record where Def = 1

I nearly got there by using the following query:

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

But I only want the GROUP BY to be applied to records where SubCatID = 1

I'm not actually sure if GROUP BY is the way to go here, it's just the closest i've seen to what I need

Output of the first query:

+-----+----------+---------------+----------+
| 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     |
+-----+----------+---------------+----------+

Output of the latter query:

+-----+----------+---------------+----------+
| 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       |
+-----+----------+---------------+----------+

This is what I want, but there are many records where GroupID=Null and Size=Null .. Therefore these dont show up, in the results. So I only want the GROUP BY to occur where SubCatID = 1

you need you use HAVING. So Something like the following:

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

MySQL Having

Here's the solution I ended up using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM