tags:

views:

17

answers:

1

Sorry if this is a duplicate question, I'm a total database newbie and I'm probably using the wrong terminology to search for answers.

I have a MySQL table as follows:

+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| placeid    | int(11)       | NO   | PRI | NULL    |       |
| grid       | varchar(120)  | YES  |     | NULL    |       |
| vill       | varchar(300)  | YES  |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+

I'd like to find out whether 'grid' and 'vill' always occur in the same combinations or not.

Maybe it'd be clearer with an example:

placeid, grid, vill
1,       TM1,  Suffolk
2,       TM1,  Suffolk
3,       WA8,  Newcastle
4,       WA8,  Newcastle
5,       WA8,  York

I'd like to construct a query that returns 'WA8' but not 'TM1', because 'WA8' occurs in combination with more than one vill.

I would be SO grateful for any help!

+2  A: 

You can do it like this:

SELECT grid
FROM MyTable
GROUP BY grid
HAVING COUNT(DISTINCT vill) > 1

It selects the grids for which there is more than one distinct vill.

Paul
How would it go if there were 10, 20 fields in table and each one could have duplicate values? Not that it's realistic scenario, but in theory?
mr.b
You mean COUNT(DISTINCT vill1, vill2, vill3...) > 1?
Paul
Awesome - thank you!
AP257
I *heart* Stack Overflow.
AP257
Quite alright :-)
Paul