我的 sql 语句在我的 sql 编辑器中运行得非常好,但是当我将它放入我的 javascript 函数中时,它告诉我我的客户端 ID 的列无效。
function getFeedposts(data) {
var limit = data.dollarLimit;
var client_id = data.salesforce_username;
var sqlQuery = `select coalesce(advertiser,'') as advertiser,
coalesce(partner,'') as partner,
coalesce(advertiser,'') || ' via ' || coalesce(partner,'') as line1,
' - SSP: blah' as line2,
' - Lead Date: ' || date as line3,
'Yesterday''s Activity' as line4,
' - Impressions: ' || coalesce(impressions,0) as line5,
' - CPM: $' || coalesce(round(ecpm,2),0) as line6,
' - Spend: $' || coalesce(round(revenue,2),0) as line7,
'Yesterday''s Spend Breakout' as line7a,
coalesce(device_type,'') as line8,
'Running Spend Totals' as line9,
' - 3 Day Spend: $' || coalesce(round(three,2),0) as line10,
' - 7 Day Spend: $' || coalesce(round(seven,2),0) as line11,
' - 30 Day Spend: $' || coalesce(round(thirty,2),0) as line12,
coalesce(round(thirty,2),0) as line13,
coalesce(round(seven,2),0) as line14,
coalesce(round(three,2),0) as line15,
coalesce(round(three,2),0) as line15,
client_id as client_id
from
(select advertiser, partner, date, impressions, ecpm, revenue, device_type, client_id
,(select sum(m.revenue)
FROM blahblah as m
WHERE m.date > rl.date -30
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as thirty
,(select sum(m.revenue)
FROM blahblah as m
WHERE m.date > rl.date -7
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as seven
,(select sum(revenue)
FROM blahblah as m
WHERE m.date > rl.date -3
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as three
from blahblah as rl
) as idunno
WHERE
date = to_date('${data.date}','mm-dd-yyyy')
and revenue > 1 and client_id ='myemail@email.com'
`;
queryRDS(sqlQuery, data);
}
这有效,但是当我这样做时
function getFeedposts(data) {
var limit = data.dollarLimit;
var client_id = data.salesforce_username;
var sqlQuery = `select coalesce(advertiser,'') as advertiser,
coalesce(partner,'') as partner,
coalesce(advertiser,'') || ' via ' || coalesce(partner,'') as line1,
' - SSP: blah' as line2,
' - Lead Date: ' || date as line3,
'Yesterday''s Activity' as line4,
' - Impressions: ' || coalesce(impressions,0) as line5,
' - CPM: $' || coalesce(round(ecpm,2),0) as line6,
' - Spend: $' || coalesce(round(revenue,2),0) as line7,
'Yesterday''s Spend Breakout' as line7a,
coalesce(device_type,'') as line8,
'Running Spend Totals' as line9,
' - 3 Day Spend: $' || coalesce(round(three,2),0) as line10,
' - 7 Day Spend: $' || coalesce(round(seven,2),0) as line11,
' - 30 Day Spend: $' || coalesce(round(thirty,2),0) as line12,
coalesce(round(thirty,2),0) as line13,
coalesce(round(seven,2),0) as line14,
coalesce(round(three,2),0) as line15,
coalesce(round(three,2),0) as line15,
client_id as client_id
from
(select advertiser, partner, date, impressions, ecpm, revenue, device_type, client_id
,(select sum(m.revenue)
FROM blahblah as m
WHERE m.date > rl.date -30
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as thirty
,(select sum(m.revenue)
FROM blahblah as m
WHERE m.date > rl.date -7
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as seven
,(select sum(revenue)
FROM blahblah as m
WHERE m.date > rl.date -3
and advertiser = rl.advertiser
and partner = rl.partner
GROUP BY partner, advertiser) as three
from blahblah as rl
) as idunno
WHERE
date = to_date('${data.date}','mm-dd-yyyy')
and revenue >
`;
sqlQuery += limit + 'and client_id =' + client_id;
queryRDS(sqlQuery, data);
}
我想知道为什么它会在这样做时告诉当前 client_id 的电子邮件地址的无效列,但是当我对其进行硬编码时,它工作得很好,任何帮助都将不胜感激
这是我收到的错误消息
RDS query successful
finished writing to rds
{ [error: column "joseph" does not exist]
name: 'error',
length: 101,
severity: 'ERROR',
code: '42703',
detail: undefined,
hint: undefined,
position: '1596',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '3090',
routine: 'errorMissingColumn' }
最佳答案
您只需将行更改为:
sqlQuery += limit + " and client_id = '" + client_id + "'";
由于 client_id 变量不在引号中,因此 SQL 语句将其视为列名,因此您会收到无效列 id 错误。
关于javascript - 为什么我的 sql 语句在编辑器中有效,但在 JS 函数中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43634345/