数组传入Mysql查询,nodejs仅返回1个结果,而不是全部

问题描述 投票:0回答:1

我在nodejs中有一个sql语句/查询应该返回3个结果,但它只返回三个结果中的第一个。 (并非所有结果)

const { custuuid } = req.params;


var LocIds = req.query.LocIds;
  console.log(LocIds);
// LocIds = 125,124,126
      let sql =
        "SELECT L.LocID,L.LocType,L.LocName,L.LocTitle,L.LocAddress,L.StartDate,L.EndDate,L.LocLat,L.LocLng FROM  ml_locations L WHERE L.LocID IN  (?) and L.CustUUID = ? Order By LocName ASC";
      const [rows] = await pool.query(sql, [[LocIds], custuuid]);
      console.log(rows);
      console.log("Done.");

生成输出:

125,124,126
[
  {
    LocID: 125,
    LocType: 1,
    LocName: 'Maui',
    LocTitle: 'Maui',
    LocAddress: 'Maui, Hawaii',
    StartDate: 2024-10-06T05:00:00.000Z,
    EndDate: 2024-10-06T05:00:00.000Z,
    LocLat: 20.7983626,
    LocLng: -156.3319253
  }
]
Done.

但是,它应该返回 3 个结果。 (124,125,126 每个位置一个)。当我在 phpMyAdmin 中手动运行 sql 时,它按预期工作:

从 ml_locations L 中选择 L.LocID、L.LocType、L.LocName、L.LocTitle、L.LocAddress、L.StartDate、L.EndDate、L.LocLat、L.LocLng,其中 L.LocID IN (124,125,126) 和 L .CustUUID = 'XXXX' 按 LocName ASC 订购

enter image description here

想法?

mysql node.js
1个回答
0
投票

看起来有点像您传递的是字符串而不是任何数组。你可以试试吗

var LocIds = req.query.LocIds.split(',').map(id => parseInt(id, 10));

const [rows] = await pool.query(sql, [LocIds, custuuid]);

检查是否能解决您的问题

© www.soinside.com 2019 - 2024. All rights reserved.