javascript - 为什么我的 sql 语句在编辑器中有效,但在 JS 函数中无效?

标签 javascript sql node.js salesforce

我的 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/

相关文章:

mysql - 如何在sql中使用自动增量在id列中添加前缀?

java - 使用 hibernate 标准查询 CLOB 列

php - nodejs 通过 ssl 连接发布空数据

javascript - 在 JavaScript 中捕获调用者的范围

javascript - JavaScript 中是否有等效的 C# String.Format()?

javascript - 如何找到修改元素的 JavaScript 片段?

javascript - 如何在 rollupjs 中禁用 Tree shaking

sql - 如何编写 sql 语句来根据另一个表中的组计算总计?

node.js - Node.js 中超出了最大调用堆栈大小

javascript - 使用 jQuery UI 制作具有可调整大小的列和可选择的行的表