• 0

[SQL] WHERE conditions with OR and AND


Question

Hey all,

Im having a bit of trouble with an SQL query.

SELECT * FROM packages WHERE package_service_id LIKE '%1%' OR package_service_id LIKE '%2%' OR package_service_id LIKE '%3%' OR package_service_id LIKE '%4%' OR package_service_id LIKE '%5%' AND package_cost <= 66

the query is meant to select all packages that include the required services and that are less than or equal to the budget. so in the example query above, its looking for all packages that include any of the 5 services, and that have a cost that is less that or equal to 66.

the problem is, it returns results that are higher than 66. If i take out the last condition, it makes no difference to the results.

Both the required services and budget are dynamic.

Im sure its something to do with me using OR and AND, but im hoping someone can shed some light on the problem :D

In the mean time, i'll be googling it :D

thank in advance

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

use brackets buddy

I literally just found that out :(

its always the case. after not being able to solve something for 15-20 minutes, i decide to post on here while i continue searching, then as soon as i post, i find the answer :(

Thanks anyway though :D

I have much to learn about SQL :(

Link to comment
Share on other sites

  • 0

sql is a mysterious beast.. once you think you have worked out it's nuances then you start getting into actually how it works under the covers, and it all changes again :D

good luck!

p.s. i would also change your query to use an IN statement if you can, looks a bit nicer :)

select
	*
from
	myTable
where id in (1,2,3,4,5)

Link to comment
Share on other sites

  • 0

I don't think SQL allows you to use IN with LIKE (and % wildcards), but I could be wrong - I've never tried :laugh:

sql is a mysterious beast.. once you think you have worked out it's nuances then you start getting into actually how it works under the covers, and it all changes again :D

good luck!

p.s. i would also change your query to use an IN statement if you can, looks a bit nicer :)

select
	*
from
	myTable
where id in (1,2,3,4,5)

Link to comment
Share on other sites

  • 0

I don't think SQL allows you to use IN with LIKE (and % wildcards), but I could be wrong - I've never tried :laugh:

no it doesn't, but i made an assumption from the query in the OP that using LIKE wasn't actually necessary..

i could be far off base though :D

Link to comment
Share on other sites

  • 0
i would also change your query to use an IN statement if you can, looks a bit nicer :)

select
	*
from
	myTable
where id in (1,2,3,4,5)

I see your query and raise you a version with even less repetition.

SELECT * FROM `table` WHERE `id` BETWEEN 1 AND 5;

Link to comment
Share on other sites

  • 0

thanks for all the replies.

I didnt know about IN or BETWEEN, however, i dont think i will be able to use them as i need to use wildcards.

the data is stored as "1,2,3,4,5" or "2,4,5" etc. so i have to use wild cards so that if someone selects service 2, it will pull out all results that include services 2 :D

Im sure if i learnt SQL a bit better i would be able to optimise some of the code i write, but it just seems like such hard work :p lol i'll learn it when i need to (Y)

Thanks again

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.