Reputation: 17
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
accession_id (INT),
date_order(DATE) DEFAULT '0000-00-00',
date_billed(DATE) DEFAULT '0000-00-00',
date_paid(DATE) DEFAULT '0000-00-00'
My Query's:
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';
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';
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
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