Jump to content

[SOLVED] Mysql query returning incorrectly


cesarcesar

Recommended Posts

The following MySql Query is not retuning correctly. It is returning dates outside the given range. Why? Thanks.

 

SELECT distinct
sale_type_landsale.sale_type_landsale_id
,sale_type_landsale.sale_date,sale_type_landsale.city,sale_type_landsale.name,sale_type_landsale.sale_price,sale_type_landsale.number_lots
FROM
sale_type_landsale
Left Outer Join connector_sale ON sale_type_landsale.sale_type_landsale_id = connector_sale.ref_id
Left Outer Join sale ON connector_sale.sale_id = sale.sale_id
Where 
sale_type_landsale.sale_date >= '2006-01-01' 
AND sale_type_landsale.sale_date <= '2007-06-08' 
AND sale_type_landsale.city ='4' 
OR sale_type_landsale.city ='13'  
AND connector_sale.ref = 'sale_type_landsale' 
AND sale.sale_archive <> 1

Link to comment
Share on other sites

solution:

SELECT distinct
sale_type_landsale.sale_type_landsale_id
,sale_type_landsale.sale_date,sale_type_landsale.city,sale_type_landsale.name,sale_type_landsale.sale_price,sale_type_landsale.number_lots
FROM
sale_type_landsale
Left Outer Join connector_sale ON sale_type_landsale.sale_type_landsale_id = connector_sale.ref_id
Left Outer Join sale ON connector_sale.sale_id = sale.sale_id
Where ( sale_type_landsale.sale_date >= '2006-01-01' AND sale_type_landsale.sale_date <= '2007-06-08' )
AND ( sale_type_landsale.city ='4' OR sale_type_landsale.city ='13' )
AND connector_sale.ref = 'sale_type_landsale' 
AND sale.sale_archive <> 1

Link to comment
Share on other sites

The parentheses around the date AND clause are not necessary (but there is nothing wrong with them, and they're fine for their visual effect).  It was the parens around the OR clause that fixed your problem.  AND "binds" more tightly than OR, so MySQL interpreted the ANDs before the OR and the ANDs after the OR and matched either of them, not both, as you had intended.

 

(Excuse me if you know this already, just posting for other's reference.)

Link to comment
Share on other sites

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.