sql - 为什么我在执行此查询时得到 2014、2015 年到 2016 年 5 月的记录 >

标签 sql

select t1.InvoiceNumber ,t1.LocalAmount  , t2.LineAmount as discount , t1.CreateDate
from CashOrderTrn t1 
left join DistrubutedDiscountDetails t2 
    on t1.InvoiceNumber =  t2.InvoiceNumber  
    and t1.CreateDate between '20160531' and  '20160701' 
    and t1.InvoiceType ='31'

最佳答案

将 t1 条件移动到 WHERE 子句:

select t1.InvoiceNumber ,t1.LocalAmount , t2.LineAmount as discount ,
      t1.CreateDate
from CashOrderTrn t1
left join DistrubutedDiscountDetails t2 on t1.InvoiceNumber = t2.InvoiceNumber
WHERE t1.CreateDate between '20160531' and '20160701' and t1.InvoiceType ='31'

顺便说一句,您是否将日期存储在字符列中?我建议改为使用 date 列。

关于sql - 为什么我在执行此查询时得到 2014、2015 年到 2016 年 5 月的记录 >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39826759/

相关文章:

php - PDO 中字符串附加问号

mysql - double not exists 子句是什么意思?

php - 从公共(public)范围获取变量以连接数据库第 2 部分

sql - PostgreSQL:将列提取到单独的表中并使用相关键更新其他列

mysql - 复杂的SQL更新语句

mysql - 如何使用MySql从数据库中获取两个表的数据

mysql - 如何使用 Mysql rand() 结果两次?

MySQL 组合键最佳实践

sql - Sybase SQL : How to check if date or timestamp field is empty

mysql - "ON DELETE SET NULL"mysql mamp 错误