询问正确的SQL查询

问题描述 投票:0回答:1
const personid = result.map(item => item.person_id)
    console.log("personen", personid)
    const formattedid = personid.join(",");
    
    const nameResult = await postJsonDataToApi({
      method: "person.get",
      body: {
        filter: {
          l: "id",
          o: "IN",
          r: `${formattedid}`,
        },
        data: ["firstname"],

      },
    });
    console.log(nameResult)
  }

在 sql 中,当我提示时,我得到了正确的答案:

SELECT firstname FROM person WHERE id IN (1,1,1,1,1,5,5); 

但是我总是收到错误,因为代码想要提示这个:

SELECT firstname FROM person WHERE id IN '1,1,1,1,1,5,5';
mysql reactjs
1个回答
0
投票

您需要确保将“formattedid”字符串传入 SQL 查询时用括号括起来。您可以修改“formattedid”变量以包含这些括号。

应调整您的代码以正确设置 SQL 查询的格式。这是代码片段的修订版本:

const personid = result.map(item => item.person_id);
console.log("personen", personid);

const formattedid = `(${personid.join(",")})`;

const nameResult = await postJsonDataToApi({
  method: "person.get",
  body: {
    filter: {
      l: "id",
      o: "IN",
      r: formattedid,
    },
    data: ["firstname"],
  },
});

console.log(nameResult);
© www.soinside.com 2019 - 2024. All rights reserved.