在不带变量的多行查询中使用 MySQL 中的值
Use a value in MySQL query in multiple lines without a variable
我有这个 mysql 查询,它使用我传递的相同值,超过 3 次。
select p.*,
ROUND(sum(unitsPerBlock * blocks) / datediff(date(now()), date(?)), 2) as avg
from batches b
inner join products p on b.productID = p.id
where (
select sum(b1.availableQty)
from batches b1
where b1.productID = p.id
and b1.addedDate between date(?) and date(now())
) = 0
and b.addedDate between date(?) and date(now())
group by b.productID
order by avg desc
有什么方法可以让我无需输入以下内容就可以进行此查询
preparedStatement.setString(1, date);
preparedStatement.setString(2, date);
preparedStatement.setString(3, date);
然后就用这个
preparedStatement.setString(1, date);
select p.*,
ROUND(sum(unitsPerBlock * blocks) / datediff(CURRENT_DATE, input.parameter), 2) as avg
from batches b
inner join products p on b.productID = p.id
CROSS JOIN (SELECT date(?) AS parameter) AS input
where (
select sum(b1.availableQty)
from batches b1
where b1.productID = p.id
and b1.addedDate between input.parameter and CURRENT_DATE)
) = 0
and b.addedDate between input.parameter and CURRENT_DATE
group by b.productID
order by avg desc
我有这个 mysql 查询,它使用我传递的相同值,超过 3 次。
select p.*,
ROUND(sum(unitsPerBlock * blocks) / datediff(date(now()), date(?)), 2) as avg
from batches b
inner join products p on b.productID = p.id
where (
select sum(b1.availableQty)
from batches b1
where b1.productID = p.id
and b1.addedDate between date(?) and date(now())
) = 0
and b.addedDate between date(?) and date(now())
group by b.productID
order by avg desc
有什么方法可以让我无需输入以下内容就可以进行此查询
preparedStatement.setString(1, date);
preparedStatement.setString(2, date);
preparedStatement.setString(3, date);
然后就用这个
preparedStatement.setString(1, date);
select p.*,
ROUND(sum(unitsPerBlock * blocks) / datediff(CURRENT_DATE, input.parameter), 2) as avg
from batches b
inner join products p on b.productID = p.id
CROSS JOIN (SELECT date(?) AS parameter) AS input
where (
select sum(b1.availableQty)
from batches b1
where b1.productID = p.id
and b1.addedDate between input.parameter and CURRENT_DATE)
) = 0
and b.addedDate between input.parameter and CURRENT_DATE
group by b.productID
order by avg desc