views:

37

answers:

1

I've got a query like this:

select a, b, c, group_concat(d separator ', ')
from t
group by a;

This seems to work just fine. As I understand it (forgive me, I'm a MySQL rookie!), it's returning rows of:

  • each unique a value
  • for each a value, one b and c value
  • also for each a value, all the d values, concatenated into one string

This is what I want, but I also want to check that for each a, the b and c are always the same, for all rows with that a value.

My first thought is to compare:

select count(*) from t group by a, b, c;

with:

select count(*) from t group by a;

and make sure they're equal. But I've not convinced myself that this is correct, and I certainly am not sure there isn't a better way. Is there a SQL/MySQL idiom for this?

Thanks!

+1  A: 

The issue with relying on MySQL's Hidden Columns functionality is spelled out in the documentation:

When using this feature, all rows in each group should have the same values for the columns that are ommitted from the GROUP BY part. The server is free to return any value from the group, so the results are indeterminate unless all values are the same.

Applied to your example, that means that the values for b and c are arbitrary -- the results can't be relied upon to consistently return the same value, and the likelihood of seeing the behavior increases with the number of possible values that b/c can return. So there's no a lot of value to compare to GROUP BY a and GROUP BY a, b, c...

OMG Ponies
I don't understand your last comment. Yes, in the real query I know that the `b` and `c` are arbitrary, which is why I want to independently confirm that each `a` has only one `b` and one `c`. If I `group by a`, that tells me how many unique `a`s there are. If I `group by a, b, c`, that tells me how many unique `(a, b, c)` triples there are. How does this lack value to my problem?
Ken
@Ken: Your comparison gives you a ballpark -- it depends on the number of different values in the column. All I see you being able to do is say "yep, GROUP BY a, b, c confirms that b and c values are possible" -- but SQL won't return values that don't exist in the column, so what's the point of the comparison?
OMG Ponies
It doesn't tell me values at all: I'm doing `count(*)` on the groupings. It tells me the number of a's is the same as the number of (a,b,c) triples. Is that not correct?
Ken