星际漫游者
发布于

数组传递到 Mysql 查询中,其中 nodejs 只返回 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 时,它按预期工作:

选择L.LocID,L.LocType,L.LocName,L.LocTitle,L.LocAddress,L.StartDate,L.EndDate,L.LocLat,L.LocLng FROM ml_locations L,其中L.LocID IN (124,125,126) 和 L.CustUUID = 'XXXX' 按位置名称 ASC 排序

浏览 (35)
点赞
收藏
1条评论
蓝海航行家
蓝海航行家
试试 var LocIds = req.query.LocIds.split(',').map(id => parseInt(id, 10)); 或者 const [rows] = await pool.query(sql, [LocIds, custuuid]);
点赞
评论