Why am I getting this result from this search query?

The sql is:

lect * from calendar where description LIKE '%investigation%' or short_name LIKE '%investigation%' AND date>='2015-08-20' AND date<='2015-08-22' group by event_id order by date

The result I get is an event that happened from 2015-06-30 to 2017-07-08. Seems like that wouldn't be a viable "return" on the above statement in that I've qualified the range of dates to be between August 20th and August 22nd.

What am I doing wrong?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
How do you know the event took place between 2015-06-30 and 2017-07-08?
You only have one date field in your query.
F IgorDeveloperCommented:
The conditions includes OR mixed with AND conditions, you need to correct like this:

select * from calendar 
where ( description LIKE '%investigation%' or short_name LIKE '%investigation%' )
AND date>='2015-08-20' AND date<='2015-08-22' group by event_id order by date 

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hieloCommented:
Try adding parenthesis around your OR expression so that the AND binds to the parenthesized expression as a whole (as opposed to just the last LIKE sub-expression):
select * from calendar where ( description LIKE '%investigation%' or short_name LIKE '%investigation%' ) AND date>='2015-08-20' AND date<='2015-08-22' group by event_id order by date
Dave BaldwinFixer of ProblemsCommented:
In addition, 'date' is a reserved word in MySQL and needs to be enclosed in backtiks to be used as a column name in a query.  https://dev.mysql.com/doc/refman/5.5/en/keywords.html  To borrow hielo's version...
select * from calendar where ( description LIKE '%investigation%' or short_name LIKE '%investigation%' ) AND `date`>='2015-08-20' AND `date`<='2015-08-22' group by event_id order by `date`

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.