选择日期会导致错误的结果

时间:2015-08-03 13:22:58

标签: mysql date

我试图从我的数据库中返回一些结果,其中date_order列是1970年,但该查询还返回了2014年和2015年的日期。我尝试了几个查询,但它完全相同。< / p>

我的表

  • accession_id (INT),
  • date_order(DATE) DEFAULT '0000-00-00',
  • date_billed(DATE) DEFAULT '0000-00-00',
  • date_paid(DATE) DEFAULT '0000-00-00'

我的查询:

  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';

1 个答案:

答案 0 :(得分:1)

您需要在第一组日期之后指定BETWEEN,并使用括号。像这样:

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'
);

现在看来,这些数字正在评估为真,并且它们已被OR化,因此您将获得所有结果。