Please consider the following simplified MySQL table:
id | docID | docTypeID | makeID | desc
1 | 1 | 1 | 1 | doc 1
2 | 1 | 1 | 2 | doc 2
3 | 2 | 2 | 1 | doc 3
4 | 3 | 3 | 2 | doc 4
5 | 1 | 1 | 4 | doc 5
6 | 2 | 2 | 5 | doc 6
7 | 4 | 1 | 1 | doc 7
In this example docID, docTypeID, and makeID are all foreign keys.
When a doc of docTypeID 1 is associated with multiple makes, I need to group those docs together and return a special make description (‘multiple makes found’).
What I need to return for this example table would be:
id | docID | docTypeID | makeID | desc
1 | 1 | 1 | multiple makes found | doc 1
3 | 2 | 2 | 1 | doc 3
4 | 3 | 3 | 2 | doc 4
6 | 2 | 2 | 5 | doc 6
7 | 4 | 1 | 1 | doc 7
Note that in the case of id 7, only a single make is found so there is no grouping performed.
I’m not sure how to limit grouping based on criteria or if that is even possible. I’m hopeful one of the SQL geniuses here can assist. Please let me know if any additional info is required. Thanks.
You can write:
(N.B. The
MIN(id)andMIN(desc)may not come from the same underlying record. If that’s a problem, then you’ll need to tweak the above a bit.)