mysql NOT in stuff..
- Started
- Last post
- 4 Responses
- mikotondria3
OK, so I have 2 tables, and I wish to list all items in table a, that are NOT in table b...
Is it as easy as my tired brain is whispering in my ear ? Havent I done this a thousand times already ?
Any answers gratefully received.
- Pupsipu0
obviously there is a 3rd table...
- mikotondria30
mmm no...
In table a are a list of bands, with each having a band_id, similarly there are items in table b that also have a band_id...
I need to know which bands in a do NOT have their band_id in b.
- Pupsipu0
lol
ok, I don't know php and databasing, but logically (what I'm thinking) you loop through your table A, and for every item in table A you loop through table B, if there is a match do something, if there isn't a match do nothing.
I don't know if php has some magic built in function that would run that quickly for you.
My solution seems sloppy, so maybe there are some databasing magic tricks you can use to organize your tables better and avoid that kind of loopage.
- sublocked0
should be able to get er done with a simple subquery.
SELECT *
FROM table_a
WHERE id NOT IN(
SELECT band_id
FROM table_b
);- formatting fail, but whateversublocked
- might want to change the "id NOT IN" to "band_id NOT IN". not really sure of your field names thosublocked
- I really think you've earned yourself a beer.mikotondria3
- i like red stripe. thx.sublocked
- Cshhk !... and other beer-opening onomatopoeiamikotondria3