Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i write sql query using BETWEEN operator and apply WHERE clause on gender_id = 1. But when i run query showing 1 data from gender_id=2 and gender_id= 1 realeted.

Following i provide query what i tryed.

What I have tried:

SELECT * FROM `wm_products` WHERE gender_id = 1 AND (pro_price BETWEEN 1000 AND 2500) OR (pro_price BETWEEN 2501 AND 5000) ORDER BY `wm_products`.`gender_id` DESC
Posted
Updated 1-Apr-19 3:00am
Comments
jaket-cp 1-Apr-19 12:11pm    
just for curiosity
could you not use
pro_price BETWEEN 1000 AND 5000
???

1 solution

It's probably a problem with your brackets (or lack of)

SELECT * FROM `wm_products` WHERE gender_id = 1 AND (pro_price BETWEEN 1000 AND 2500) OR (pro_price BETWEEN 2501 AND 5000) ORDER BY `wm_products`.`gender_id` DESC


You are saying where gender_id = 1 and (ABC) or (XYZ)

so if XYZ is true then that data is included in your subset as it is independent from the AND between gender_id and ABC. You probably want something like this

where gender_id = 1 and ((ABC) or (XYZ))

so the "or" is its own comparison and the result is then ANDed with gender_id

SELECT * FROM `wm_products` WHERE gender_id = 1 AND ((pro_price BETWEEN 1000 AND 2500) OR (pro_price BETWEEN 2501 AND 5000)) ORDER BY `wm_products`.`gender_id` DESC
 
Share this answer
 
v2
Comments
jaket-cp 1-Apr-19 12:12pm    
my 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900