My Tables:
CREATE TABLE `binary` (
`binaryid` int(15) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`binaryid`)
);
CREATE TABLE `binarycollection` (
`binaryid` int(10) unsigned NOT NULL,
`collectionid` int(10) unsigned NOT NULL,
UNIQUE KEY `collectionid` (`collectionid`,`binaryid`),
KEY `binaryid` (`binaryid`)
);
In the binary table there can only exist one record to a binaryid.
The binarycollection table ties the binary to multiple collections.
What I need to do is make a query that will select all rows in binary that have exactly 1 relations in binarycollection.
So given the example:
binary:
1
2
3
4
5
6
7
binarycollection:
(binaryid collectionid)
1 1
2 1
3 1
3 2
4 1
4 2
5 2
6 2
It should return binaryids 1, 2, 5, and 6.
Any help is appreciated. 🙂
ps. This needs to be efficient, the tables contain millions of rows.
Use
GROUP BY: