PDA

View Full Version : Ignore rows in MySQL query?



mentha
October 26th, 2007, 09:39 AM
I'm working on a simple guestbook with IP-blocking possibility. Therefor I've created 2 tables, 1 for the guestbook itself, which also logs the visitors IP and 1 with the banned IP's. So far everything works as planned. I can block an IP, after which I get a message in my guestbook backend mentioning that the IP has been banned.

Now I want to start building the frontend, but I can't figure out the right query to create for getting all rows EXCEPT the ones that have the banned IP. I can get all rows and then in my WHILE statement (mysql_fetch_array) skip the ones that are banned, but that means that I don't get the right total of entries per page. LIMIT 10 for example gives 10 rows, but if 3 of them have a banned IP, I only get 7 entries on my page and I want 10...

Any ideas? Thanks in advance!

Charleh
October 26th, 2007, 09:57 AM
SELECT * FROM Guestbook WHERE IP NOT IN (SELECT IP FROM Bannedlist)

Gundark
October 26th, 2007, 10:04 AM
You can easily do this with a sub-query, just use IN. Something like this:



SELECT * FROM guestbook WHERE IP NOT IN (SELECT IP FROM bannedips)


Edit: Darn, leave to use the bathroom get beaten to the punch...

Charleh
October 26th, 2007, 10:24 AM
Muhahahah! I've got nothing better to do at work :)

mentha
October 26th, 2007, 07:27 PM
Awesome! Thanks guys for the quick (and simultaneous) replies! This saves me 3 days of thinking, haha! And again, my knowledge grows further... ;)