How can I assign one part of the WHERE clause to multiple other parts of it?
I would like to make a mysql query like: SELECT * FROM something WHERE title LIKE '%".$_POST["search"]."%' OR something LIKE '%".$_POST["search"]."%' AND costs < '".$max."'
This will give me all the results where title like search and the results for something like search AND costs smaller $max. But I want the costs < $max also for title like $search. But I 开发者_如何学Godont want to put costs < $max two times into the script. So is there a way to assign it to both? Maybe with brackets: WHERE (title LIKE '%".$_POST["search"]."%' OR something LIKE '%".$_POST["search"]."%') AND costs < '".$max."'
How can I achieve this?
Thank you! phpheini
Yes, you can use parentheses so that the OR is evaluated first:
WHERE (title LIKE '%foo%' OR something LIKE '%foo%')
AND costs < '20'
Other remarks:
- I'd suggest that you escape your strings using
mysql_string_real_escape
to prevent SQL injection attacks. Or take a look at PDO and prepared statements. - You may also want to look at a full-text search as an alternative approach as this will be faster.
精彩评论