Query for pulling a banner based on a flag
Posted: Sun Mar 07, 2010 11:24 am
I'm trying to write a query where a member can rotate banner advertisements only if a flag called allow_banners is = 1 in the users table.
To select the banner my query is simple:
But to add in the user flag to the query it gets complicated (seems like it should be easy, maybe I've been working too much).. something like
This feels really ugly. It might work but I haven't tried it due to how ugly it feels. Is there a more elegant solution or is that right?
To select the banner my query is simple:
Code: Select all
SELECT * FROM `banners` ORDER BY RAND() LIMIT 1Code: Select all
SELECT * FROM `banners` WHERE `user_id` IN(SELECT `id` FROM `users` WHERE `allow_banners` = 1) ORDER BY RAND() LIMIT 1