javascript 如何在多个where子句中为sql查询赋值并在单个命令中运行查询

2w3kk1z5  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(127)

我的值数组为:

const params = [
['2022-12-10', 'aaaaa', '2022-12-01', 'xhxha', '2022-12-10'],
['2022-12-11', 'ababa', '2022-12-01', 'xhxha', '2022-12-11'],
['2022-12-12', 'acaca', '2022-12-01', 'xhxha', '2022-12-12'],
['2022-12-13', 'adada', '2022-12-01', 'xhxha', '2022-12-13'],
];

const data = await db.query(`select id, title, DATE_FORMAT(end_date,"%Y-%m-%d") as end_date ABS(DATEDIFF(?, end_date))+1 as delay from chart 
where uid = ?
and date = ?
and project_uid = ?
and end_date = ?
and completed is true;
`, [params]);

我想运行此查询,但所有值都被添加到第一个?(问号)。我想将这些值设置为不同的?并获得结果。
我也不想在for循环中运行查询。我如何在这里得到结果?

p8h8hvxi

p8h8hvxi1#

您可以连接到参数子查询。从8.0开始,您可以使用VALUES表构造函数。或者在params子查询中使用SELECT .. UNION ALL

select id, title, DATE_FORMAT(end_date,"%Y-%m-%d") as end_date ABS(DATEDIFF(?, end_date))+1 as delay
from (
  values
   row(date '2022-12-10', 'aaaaa', date '2022-12-01', 'xhxha', '2022-12-10'),
   row(date '2022-12-11', 'ababa', date '2022-12-01', 'xhxha', '2022-12-11'),
   row(date '2022-12-12', 'acaca', date '2022-12-01', 'xhxha', '2022-12-12'),
   row(date '2022-12-13', 'adada', date '2022-12-01', 'xhxha', '2022-12-13')
) params(p1, p2, p3, p4 ,p5)
join chart c
on c.uid = params.p1
and c.date = params.p2
and c.project_uid = params.p3
and c.end_date = params.p4
and c.completed is true

相关问题