error: syntax error at or near "S" node posgres

error: syntax error at or near "S" node posgres

我的问题是我无法解决错误 42601,我知道这是一个语法错误。我 运行 在 pgadmin 上查询,它工作正常。这是我的代码 我需要串联查询过程来解决用户的不同请求。

ps。我正在使用节点 posgres promise

return new Promise((resolve,reject)=>{
    let {searchType , searchValue , sortType , filterType , size , index} = req.params;

    var select_clause = `SELECT svn_id , mobile_number , network_prefixes.prefix , network_prefixes.country_name , date_created , 
        date_subscribed , expiry_date , svn_status , 
        (select count(*) from svn_transactions where svn_status = 'ACTIVE') "total_active_svn", 
        (select count(*) from svn where status = 'UNASSIGNED') "total_available_svn", 
        (select count(*) from svn_transactions where (svn_status = 'INACTIVE' OR svn_status = 'INCOMING_SMS_ONLY')) "total_grace_svn"
        FROM svn_transactions 
        LEFT JOIN network_prefixes ON network_prefixes.prefix = substr(svn_transactions.mobile_number,1,length(network_prefixes.prefix)) `;

    var where_clause;
    switch(searchType){
        case 'MOBILE_NUMBER':
            where_clause = `WHERE (mobile_number LIKE '%' ||  || '%') `;
            break;
        case 'SVN_NUMBER':
            where_clause = `WHERE (svn_id LIKE '%' ||  || '%') `;
            break;
        default: //ALL
            where_clause = `WHERE (mobile_number LIKE '%' ||  || '%') OR (svn_id LIKE '%' ||  || '%') `;
    }

    var sort_type;
    switch(sortType){
        case 'MOBILE_NUMBER':
            sort_type = `order by mobile_number `;
            break;
        case 'SVN_NUMBER':
            sort_type = `order by svn_id `;
            break;
        case 'RENEWAL_DATE':
            sort_type = `order by date_subscribed `;
            break;
        case 'SUBSCRIPTION_DATE':
            sort_type = `order by date_created `;
            break;
    }

    var pagination = ` limit  offset `;

    var query = `${select_clause} ${where_clause} ${sort_type} ${filterType} ${pagination}`;

    pool.query(query[  
        searchValue , size , index
    ],function(err,result){
        if(err) {
            console.log(err);
            reject(err);
        }resolve(result);
    });
});

响应错误为:

error: syntax error at or near "S"
at Connection.parseE (/var/www/appvno/admin/node_modules/pg/lib/connection.js:614:13)
at Connection.parseMessage (/var/www/appvno/admin/node_modules/pg/lib/connection.js:413:19)
at Socket.<anonymous> (/var/www/appvno/admin/node_modules/pg/lib/connection.js:129:22)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Socket.Readable.push (_stream_readable.js:212:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
length: 89,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '1',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1134',
routine: 'scanner_yyerror'}

我试过将它们组合成一个查询,但效果很好。:

var example_query = `SELECT svn_id , mobile_number , network_prefixes.prefix , network_prefixes.country_name , 
        date_created , date_subscribed , expiry_date , svn_status , 
        (select count(*) from svn_transactions where svn_status = 'ACTIVE') "total_active_svn",
        (select count(*) from svn where status = 'UNASSIGNED') "total_available_svn",
        -- total_recycled_svn 
        (select count(*) from svn_transactions where (svn_status = 'INACTIVE' OR svn_status = 'INCOMING_SMS_ONLY')) "total_grace_svn"
    FROM svn_transactions 
    left join network_prefixes on network_prefixes.prefix = substr(svn_transactions.mobile_number,1,length(prefix)) 
    WHERE (mobile_number LIKE '%' ||  || '%')
    order by mobile_number DESC
    limit  offset `;

我研究了 select 查询的串联是否有限制,但我一无所获。任何帮助都可以,谢谢!

编辑 1:我已经检查过空格是否正确并且没有丢失分号...基于 42601 的含义

编辑 2:这是我的 console.log 串联查询

SELECT svn_id , mobile_number , network_prefixes.prefix , network_prefixes.country_name , date_created ,
        date_subscribed , expiry_date , svn_status ,
        (select count(*) from svn_transactions where svn_status = 'ACTIVE') "total_active_svn",
        (select count(*) from svn where status = 'UNASSIGNED') "total_available_svn",
        (select count(*) from svn_transactions where (svn_status = 'INACTIVE' OR svn_status = 'INCOMING_SMS_ONLY')) "total_grace_svn"
        FROM svn_transactions
        LEFT JOIN network_prefixes ON network_prefixes.prefix = substr(svn_transactions.mobile_number,1,length(network_prefixes.prefix))  WHERE (mobile_number LIKE '%' ||  || '%')  order by mobile_number  DESC  limit  offset 

在 pgadmin 上仍然工作正常,但在节点 postgres 上仍然出现错误 42601

这部分:

`SELECT svn_id , mobile_number , network_prefixes.prefix , network_prefixes.country_name , 
    date_created , date_subscribed , expiry_date , svn_status , 
    (select count(*) from svn_transactions where svn_status = 'ACTIVE'

在 'ACTIVE' 使用 \ ' 代替 '(\ 和 ' 之间没有空格)

此外,为什么要使用 ` 来分隔字符串?不应该是 ' ?

我发现了错误。这不是查询,而是错误是我的 pool.query 设置。我只是忘记在查询之后和值之前添加逗号。呵呵傻我 之前:pool.query(查询[.. 之后:pool.query(查询,[..