Jeremy Burchwell
Jeremy Burchwell

Reputation: 17

Select Date delivers wrong results

I'm trying to return some results from my database where the column date_order is 1970 but the query also returns dates from 2014 and 2015. I have tried a couple query's but its all the same.

My table

My Query's:

  1. SELECT DISTINCT(accession_id), date_order, date_billed, date_paid FROM billing WHERE date(date_order) = '1970-01-01' AND accession_id BETWEEN '206070' AND '296715' OR '402603' AND '411626';

  2. SELECT DISTINCT(accession_id), date_order, date_billed, date_paid FROM billing WHERE date_order = '1970-01-01' AND accession_id BETWEEN '206070' AND '296715' OR '402603' AND '411626';

  3. SELECT DISTINCT(accession_id), date_order, date_billed, date_paid FROM billing WHERE date_order LIKE '%1970%' AND accession_id BETWEEN '206070' AND '296715' OR '402603' AND '411626';

Upvotes: 0

Views: 39

Answers (1)

trex005
trex005

Reputation: 5115

You need to specify your BETWEEN after the first set of dates, and use parenthesis. Like so:

SELECT DISTINCT(accession_id), date_order, date_billed, date_paid FROM billing WHERE date(date_order) = '1970-01-01' AND 
(
  accession_id BETWEEN '206070' AND '296715'
  OR
  accession_id BETWEEN '402603' AND '411626'
);

As it stands now, those numbers are evaluating to true, and they are ORed in, so you will get all the results.

Upvotes: 1

Related Questions