SQL WHERE子句不起作用

时间:2015-11-20 01:50:50

标签: sql postgresql where amazon-redshift

我有以下SQL脚本。这很简单,不幸的是只有where子句决定了哪些广告系列正在运行。

SELECT
    a.id,
    a.leadid,
    a.campaign_name__c,
    cast(a.firstrespondeddate as date),
    b.status,
    cast(b.createddate as date),
    b.ownerid
FROM rjm_current.sf_campaignmember a 
    LEFT JOIN rjm_current.sf_lead b ON a.leadid = b.id
WHERE a.campaign_name__c LIKE '%demo%' OR a.campaign_name__c LIKE '%contact%'
    AND cast(a.firstrespondeddate as date) <> '7/14/2015'
    AND b.status = 'New' 
    AND a.contactid IS NULL
    AND a.leadid IS NOT NULL

任何帮助非常感谢。在另一个查询中,我必须将firstrespondeddate转换为一个表中的日期并使用另一个表来表示firstrespondeddate <> '7/15/2015',但它应该可以工作而无需创建一堆表。

1 个答案:

答案 0 :(得分:1)

OR表达式应放在括号中:

WHERE (a.campaign_name__c like '%demo%' OR a.campaign_name__c like '%contact%')
and cast(a.firstrespondeddate as date) <> '7/14/2015'
and b.status = 'New' 
and a.contactid is null
and a.leadid is not null

请参阅Operator Precedence