snowdog Posted April 27, 2007 Share Posted April 27, 2007 I have an sql query and I need some help. Here is my query $query = "SELECT * FROM call_reminder WHERE date >= '$todays_date' AND date >= '$start_date' AND date <= '$end_date' AND call = 'Bonus Call' AND level = 1 AND member_group = '$member_group' OR member_group = 'all' ORDER BY date ASC"; basically if it was a math equation I would put brackets here to force it to be: $query = "SELECT * FROM call_reminder WHERE date >= '$todays_date' AND date >= '$start_date' AND date <= '$end_date' AND call = 'Bonus Call' AND level = 1 AND (member_group = '$member_group' OR member_group = 'all') ORDER BY date ASC"; It is not doing the sql the way I want it to. I want it to act like a math equation in the brackets. DOes anyone know how to make this happen? I know there is lots of AND's but I need the OR part to work also incase there is twp Bonus calls in the same time frame (say two months) and only one group is supposed to see one of them(ie: oriebtation call for a new group), then all groups could see the 2nd one. Thanks in advance. Snowdog Link to comment https://forums.phpfreaks.com/topic/49008-mysql-and-or-questions/ Share on other sites More sharing options...
sanfly Posted April 27, 2007 Share Posted April 27, 2007 I think for something like this you would need to use UNION $query = "(SELECT * FROM call_reminder WHERE date >= '$todays_date' AND date >= '$start_date' AND date <= '$end_date' AND call = 'Bonus Call' AND level = 1 AND member_group = '$member_group') UNION (SELECT * FROM call_reminder WHERE date >= '$todays_date' AND date >= '$start_date' AND date <= '$end_date' AND call = 'Bonus Call' AND level = 1 AND member_group = 'all') ORDER BY date ASC"; Also, if you have any trouble with the date fieldname, put backticks around date eg: `date` (MYSQL Reserved Words) Link to comment https://forums.phpfreaks.com/topic/49008-mysql-and-or-questions/#findComment-240051 Share on other sites More sharing options...
snowdog Posted April 27, 2007 Author Share Posted April 27, 2007 Thanks I will give that a try, I have never heard of that before. I just figured there was something simpole I was missing. I am self taught and it ias hard for me to sometimes explain or what I am looking for because I dont know what I am searching for. Thanks again Snowdog Link to comment https://forums.phpfreaks.com/topic/49008-mysql-and-or-questions/#findComment-240104 Share on other sites More sharing options...
Barand Posted April 27, 2007 Share Posted April 27, 2007 You had it. When you use a mix of AND and OR, use (..) to clarify the logic eg WHERE A AND (B OR C) or WHERE (A AND B) OR C Link to comment https://forums.phpfreaks.com/topic/49008-mysql-and-or-questions/#findComment-240138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.