Wouldn't this involved a sub-query? i.e. select * from images where ( select * from categories where cat_id = 3 AND cat_id != 5) Now, the question is, do the more recent versions of mysql support subqueries?
Take a look at: http://www.mysql.com/documentation/mysql/bychapter/manual_Introduction.html#ANSI_diff_Sub-selects This may help you out in potentially re-writting your query? possibly: select images.* from images LEFT JOIN categories on images.id = categories.id where categories.cat = 3 AND catdgories.cat != 5 HTH -Brad David Freeman wrote: > Hi All > > This is probably off-topic here and my only real excuse is that after > having searched google as well as both php and mysql sites/manuals I'm > no closer to an answer for something I'd like to get solved this > morning... > > I have two database tables (mysql). One contains information about > images (filename, image description, directory path), the other contains > two columns - one is the ID of the image and the other is an ID that > represents an image category. > > The reason for doing this is so that I can have an image in multiple > categories. Ie. An Image is a product image and also a promotional > image (for example). > > Now I want to randomly select an image from my database such that it > belongs in a set category. This isn't a problem and I have the code > working fine. I am having trouble with the twist on this though. I > want to be able to select an image based on it being in a certain > category but also to be able to exclude that image if it is in a > different category. That is, I want to select images in category 3 but > only if they are not also in category 5 (for example). > > Can this be done in a single mysql query or am I going to have to link a > series of mysql queries together with some php code to firstly get a set > of images that are in one category (into an array for eg) and then go > through and remove anything that's also in another category and then do > a random selection from the remaining contents of the array? > > I'd like to think it can be done in a single mysql query and it would > certainly be more elegant but I haven't been able to figure out a way > and I'm starting to think I'll need to do it by manipulating arrays in > php which would be far less elegant (although obviously still do-able). > > Any suggestions anyone? Or references to a tutorial/website somewhere > that talks about doing this sort of thing would also be fine. > > Thanks > Dave > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php