2015-05-11 75 views
1

我使用Postgres的,我想尝试date_diff,但我得到这个错误
Postgres的警告:pg_query():查询失败:ERROR:运营商不存在:布尔=整数LINE 1

Warning: pg_query(): Query failed: ERROR: operator does not exist: boolean = integer LINE 1

和那么这是我的查询

select id_mitra 
     ,company_name 
from ref_mitra 
where reject=0 and DATE_DIFF(now(),date_status) > 3 and active='N' 

我的查询出了什么问题?

+0

显示你的表的描述请。也许'reject'是一个布尔值。 – Jens

+0

如果要使用值0(零),则必须将此值转换为布尔值:reject = CAST(0作为布尔值) –

+0

您需要将布尔值与布尔值“where reject = false”进行比较。 Postgres中也没有'date_diff'函数。 –

回答

1

你的价值0投射到布尔:

select id_mitra 
     ,company_name 
from ref_mitra 
where reject = CAST(0 as boolean) 
and DATE_DIFF(now(),date_status) > 3 
and active='N'; 

或者使用一个布尔值:

select id_mitra 
     ,company_name 
from ref_mitra 
where reject = false 
and DATE_DIFF(now(),date_status) > 3 
and active='N'; 

DATE_DIFF()不是PostgreSQL中的标准功能,但也许你用这个创建的函数名称。

+0

你确定'date_diff'是可用的。在PostgreSQL中? –

+0

@vivek:不,我可以用CREATE FUNCTION –

+0

智能回复:) –

2

您的查询的两个错误:

  1. 你是一个号(0)比较的boolean
  2. 没有date_diff()功能的Postgres

假设date_status是(实际)date列,您的查询应该是这样的:

select id_mitra 
     ,company_name 
from ref_mitra 
where reject = false 
    and current_date - date_status > 3 
    and active='N'; 

now()返回timestamp但你显然需要的天数(不是一个interval),你应该使用current_date而不是now()

我不知道是什么date_diff()真的呢,也许你需要写date_status - current_date才能得到相同的行为。

而不是reject = false你也可以使用where not reject

+0

这是我要回答的:) –

+0

使用NOT作为布尔值是一个不错的诀窍,但只有当NOT不是查询参数时:不能使用准备好的语句,其中NOT取决于用户输入。 –

相关问题