fife Posted February 13, 2012 Share Posted February 13, 2012 I have a query that is very messy. I have tried using () to seperate everything and when I do it just all goes wrong. here is the query SELECT posts.*, users.*, countries.countryID, countries.country FROM posts INNER JOIN users ON users.userID = posts.postUserID INNER JOIN countries ON countries.countryID = users.userCountry WHERE posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'sightseeing' OR posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'Inner city sightseeing' OR posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'Outer city sightseeing' ORDER BY posts.postID DESC Is there a way of re-writing this so that it has brackets seperating everything and it will still work. It works for the minute but re- writing posts.cityID = '".$row_rs_city['cityID']."' inbetween every OR just seems wrong. Thanks for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/257040-nesting-operators/ Share on other sites More sharing options...
Adam Posted February 13, 2012 Share Posted February 13, 2012 Your WHERE condition doesn't make much sense: SELECT posts.*, users.*, countries.countryID, countries.country FROM posts INNER JOIN users ON (users.userID = posts.postUserID) INNER JOIN countries ON (countries.countryID = users.userCountry) WHERE posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'sightseeing' OR posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'Inner city sightseeing' OR posts.cityID = '".$row_rs_city['cityID']."' AND posts.type = 'Outer city sightseeing' ORDER BY posts.postID DESC What's the logic behind it, in English? That very much dictates where the parentheses should go. Quote Link to comment https://forums.phpfreaks.com/topic/257040-nesting-operators/#findComment-1317599 Share on other sites More sharing options...
fife Posted February 13, 2012 Author Share Posted February 13, 2012 Ive solved it with a new operator I'd never used or seen before. Here is the fixed query SELECT posts.*, users.*, countries.countryID, countries.country FROM posts INNER JOIN users ON users.userID = posts.postUserID INNER JOIN countries ON countries.countryID = users.userCountry WHERE posts.cityID = '".$row_rs_city['cityID']."' AND posts.type IN ('sightseeing', 'Inner city sightseeing', 'Outer city sightseeing') ORDER BY posts.postID DESC Thanks anyway guys Quote Link to comment https://forums.phpfreaks.com/topic/257040-nesting-operators/#findComment-1317605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.