I’m having a problem with mysql that you might be able to help. I have 2 tables with the following structure, and a couple of sample rows
TABLE 1
-------
LISTID NAME 10_OTHER_COLUMNS
---------------------------------------
1 List1
2 List2
3 List3
TABLE 2
-------
LISTID LISTTYPE(ENUM, 4 options)
------------------------------------------------
1 type1
1 type2
2 type3
3 type1
3 type2
3 type3
The relation is one to many from TABLE 1 to TABLE 2. I want to do a select on TABLE 1 where the rows are return ONLY, and ONLY IF they have exactly one match on TABLE 2. Explaining better, a list may have more than one type, if I simply do a:
SELECT t1.*
FROM table_1 t1
LEFT JOIN table_2 t2 USING (listid)
WHERE t2.listtype = 'type3'
It returns List2 and List3. I want to remove List3 from the results and only have List2. Any ideas?
Ok, I THINK I got it… You are asking for ex: “type3”, an English description to clarify would be…
Give me a list of all Tables (ie: ListID) that AT MOST have ONLY the one code I’m looking for and nothing else, not associated with any other “types”.
FEEDBACK OPTION…
Per your comment about a couple million records, I would adjust it this way which may also be faster overall too.
I’d like to also know the performance difference on the second one. This second approach actually works based on an INTENTIONAL expected “don’t find me”, but doesn’t require a sub-select. By joining table2 to itself on same ID, but an “any other” type than the one we’re looking for, the ONLY one we want is the entry where WE DONT find in the t2Extra instance. THEN, get the table name from t1…