I have a load of code in place to ‘filter’ some records based on if a certain field appears in a list.
Currently SELCT * FROM table1 ... AND value IN (8, 6, 4, 2, 9, 1, 3, 5, 7) ...
With table1 structure :
CREATE TABLE IF NOT EXISTS `table1` (
`uniqueid` varchar(32) NOT NULL,
`value` int(11) DEFAULT NULL,
...
PRIMARY KEY (`uniqueid`)
)
Now I have to store value in a separate table and there may well be more than one ‘value’ in the table.
New table1 structure:
CREATE TABLE IF NOT EXISTS `table1` (
`uniqueid` varchar(32) NOT NULL,
...
PRIMARY KEY (`uniqueid`)
)
New table2 structure
CREATE TABLE IF NOT EXISTS `table2` (
`id` int(11) NOT NULL,
`uniqueid` varchar(32) NOT NULL,
`value` int(11) DEFAULT NULL,
...
PRIMARY KEY (`id`)
)
You get the idea!
My question is how to check if any record in table2 for uniqueid is in the list given in the first example.
I considered using some sort of COUNT subquery but I’m not sure. I tried:
SELECT * FROM table1, table2
WHERE table1.uniqueid = table2.uniqueid
AND (SELECT COUNT(id) FROM table1, table2
WHERE table2.uniqueid = table1.uniqueid ) > 0
AND table2.value IN (8, 6, ...
But it wasnt working and I don’t really like the approach, feels very messy.
Any help appreciated.
If you wanted to find those values that exist in 2 tables, implement an
INNER JOIN.