Jump to content

Recommended Posts

I am trying to run a query something like this:

from WHERE clause :

 

WHERE ( s.subject_name LIKE '%$keyword%' OR
c.city_name LIKE '%$keyword%' OR
o.option_name LIKE '%$option%' OR
d.district_id = $districtId OR
c.city_id = $cityId
)

 

With this conditions, first two are compulsory ($keyword from subject_name or city_name) and other 3 are optional. The query not meet last three conditions result should comes with $keyword matching in subject_name or city_name. If $option have a value result should come with matching $keyword and $option. Further if $districtId have a value result should become from matching $keyword, $option and $districtId. Finally if $cityId have a value query should check all conditions to return records.

 

My problem is when I create WHERE clause like above in my query it is not meeting my criteria.

 

Can anybody help me to do this?

Thank you.

Edited by thara
Link to comment
https://forums.phpfreaks.com/topic/274690-check-conditions-in-where-clause/
Share on other sites

With this conditions, first two are compulsory ($keyword from subject_name or city_name) and other 3 are optional.

No, you've ORed all of them together which means that at least one of the five has to match. There are no restrictions on which.

 

Don't use conditions that you don't want to check. If $option doesn't have a value then don't include it at all.

I'm totally agree with reguinix.

 

@thara, take a look for a moment at that table,

 


Intermediate result				 Final result

WHERE TRUE OR TRUE		 TRUE

WHERE FALSE OR FALSE	 FALSE

WHERE FALSE OR TRUE		 TRUE

WHERE TRUE OR FALSE		 TRUE

No, you've ORed all of them together which means that at least one of the five has to match. There are no restrictions on which.

 

Don't use conditions that you don't want to check. If $option doesn't have a value then don't include it at all.

 

 

My conditions are...

  • $keyword is always should true. Other 3 may true or not.
  • Only $keyword have a value return records should come matching with $keyword insubject_name or city_name.
  • If $optional have a value then return records should come matching with $keyword and $option
  • If $districtId have a value then result should come matching in 3 variable values. ($keyword,$option, $districtId)
  • Finally if $cityId have a value along with others return result should come with matching all 4 variables' values.

This conditions come from my search page. $keyword is text box value and its always need a keyword to start searching. That's why I said it is always true. Other 3 values come from select box that select the items when they needed.

 

I tried to make the query, its ok. but really confusing in WHERE clause. Above code is from my WHERE clause so far. But It can not get my desired result.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.