Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 我有2个日期字段,我希望记录可以分为1个或两个字段_Sql_Date_Where - Fatal编程技术网

Sql 我有2个日期字段,我希望记录可以分为1个或两个字段

Sql 我有2个日期字段,我希望记录可以分为1个或两个字段,sql,date,where,Sql,Date,Where,我有两个日期字段,我试图在where子句中使用OR。例如, Select customer_records.customer_id, customer_records.join_date, rejected_customers.rejected_date, status_lookup.status_description From customer_records, rejected_customers, status_lookup Where status_lookup.status_id(

我有两个日期字段,我试图在where子句中使用OR。例如,

Select customer_records.customer_id, customer_records.join_date, rejected_customers.rejected_date, status_lookup.status_description 
From customer_records, rejected_customers, status_lookup
Where status_lookup.status_id(+) = customer_records.status_id and customer_records.customer_id = rejected_customers.customer_id
and (customer_records.join_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy') or rejected_customers.rejected_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')

因此,基本结果应该是,当加入日期或拒绝日期落在我的日期字段中时,我需要客户的id。我似乎无法让它工作,任何帮助感谢,谢谢

当你说你似乎无法让它工作时,我假定你运行了查询,没有得到预期的结果,也没有得到任何错误。
Select cr.customer_id, cr.join_date, rej.rejected_date
from customer_records cr left join rejected_customers rej on cr.customer_id=reg.customer_id
where (cr.join_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')) or (rej.rejected_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy'))
有时,一些日期字段以时间分数存储在数据库中。所以你需要使用“trunc”函数。请参见Oracle数据库中的此示例:

SQL> select to_char(sysdate,  'dd/mm/yyyy hh:mi:ss') date_no_trunc from dual;

DATE_NO_TRUNC
-------------------
28/03/2013 10:04:27

SQL> select to_char(trunc(sysdate),  'dd/mm/yyyy hh:mi:ss') date_with_trunc from dual;

DATE_WITH_TRUNC
-------------------
28/03/2013 12:00:00

SQL> 
因此,您需要将查询更改为如下所示:

Select c.customer_id,
       c.join_date,
       r.rejected_date
From   customer_records c,
       rejected_customers r
Where  c.customer_id = r.customer_id
and   ( trunc(c.join_date) between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')
        or
        trunc(r.rejected_date) between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')
      )

另外,给长表名加别名总是一个好主意,这会使查询更短,占用更少的内存。

当您说无法让它工作时,您是什么意思?您是否收到错误消息,或错误数据,或什么?还有,您使用的是什么RDM?oracle 10,我没有收到错误消息,但我收到的数据不正确。我只获得客户记录。加入日期,而不是完整的列表。刚刚看到上面的评论-此信息将非常有帮助。但是,我猜您只有在发生拒绝的情况下才有拒绝的记录,因此常规连接将只匹配有拒绝的记录。左连接(这是MS-SQL语法)将包括所有记录,如果拒绝表中没有匹配项,则拒绝日期为NULL。你也应该给你的表加上别名(例如
cr
而不是
customer\u records
),因为这样更容易理解。嗨,克里斯·K,我想我需要给你更多的信息。在执行您的建议时,我收到一个错误:或的操作数中不允许使用外部联接运算符(+)。我不知道外部连接会影响这种情况,因为我认为这只是我的逻辑错误。我更新了代码以反映外部联接。好的,我将尝试此操作,谢谢。(我会立即回复,但我必须出去开会。)TRUC没有改变任何东西。请从您的表格中发布一些示例行,并说明您希望从示例行中获得什么。这将使我们更好地了解你的问题。