Where condition not working fine

SELECT a.area_name,c.category_name,b.borough_name,prb.pnb_code,
                  prb.category_of_bussiness,
                  prbl.type_of_bussiness,prb.date_of_data,
                  prb.category_of_bussiness,
                  prb.business_name,
                  prb.applicant_ownername,
                  prb.applicant_owner_address,
                  prb.contact,
                  prb.email,
                    prb.date_of_data,
                   prb.otherproperties_owned1,
                    prb.otherproperties_owned2,
                    prb.otherproperties_owned3,
                    prb.agent_name,
                    prb.agent_contact,
                    prb.agent_email,
                    prb.agent_address,
                    prb.applicant_form_link,
                    prb.website_used,
                  prb.notice,
                  prb.keyword,prb.approve,prb.applicant_post_code
                  FROM  property_related_bussiness as prb
                  INNER JOIN area as a on a.area_code = prb.area_code
                  INNER JOIN category as c on c.cat_code = prb.cat_code
                  INNER JOIN borough as b on b.borough_code = prb.borough_code
                  INNER JOIN property_related_bussiness_list as prbl
                  ON prbl.prb_id = prb.type_of_bussiness
                   WHERE prb.approve = '0' OR prb.approve = '2'
                   AND prb.status ='0'
                  ORDER BY prb.pnb_code DESC

i use this select query prb.approve = ‘0’ OR prb.approve = ‘2’ this two condition working fine but AND prb.status =‘0’ this one not working anybody reply me.

actually, it’s working exactly as you coded it

ANDs take precedence over ORs

so what you coded is evaluated like this –

WHERE prb.approve = '0' 
   OR (
      prb.approve = '2'
  AND prb.status ='0'
      )

do understand that? the AND is evaluated first, then the OR

whereas i believe what you want is this –

WHERE (
      prb.approve = '0' 
   OR prb.approve = '2'
      )
  AND prb.status ='0'

note that it’s easier and simpler if you code it like this –

WHERE prb.approve IN ( '0' , '2' )
  AND prb.status ='0'
1 Like

Is this a precedence issue that could be solved by using parentheses?

i.e. is the logic
(approve = 0 OR approve = 2) AND status = 0
or is it
approve = 0 OR (approve = 2 AND status = 0)

EDIT
Ninja’d :smiley_cat:

Again same issue it’s not working fine.

Is it
“bussiness” != “business” ?

please show the entire WHERE clause that you tried

@rkrajeshkannanmca,

I think one of your table failed to use inner join, try some left join or right join.

ya it’s correct.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.