postgresql - SpringBoot-使用PostgreSQL的多个参数的单值
问题描述
PostgreSQL 查询
select date,count(*) from table1
where (DATE_PART('day', current_date::timestamp - table1.date::timestamp ))
<(SELECT value from keyvaluepair where priority='Important')
and priority = 'Important' group by date
order by date
我将从哪里调用 api
http://localhost:8080/get/getDates?priority=Important
我会通过吗?如图所示在两个地方
select date,count(*) from table1
where (DATE_PART('day', current_date::timestamp - table1.date::timestamp))
<(SELECT value from keyvaluepair where priority=?)
and priority = ? group by date
order by date
我收到这样的错误
No value specified for parameter 2.; nested exception is org.postgresql.util.PSQLException: No value specified for parameter 2.
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback;
在java中,我只是得到这个查询数据的日期和计数。
public Map<String,Object> getDates(String priority) {
Map<String,Object> map = new Map<String,Object>();
List<Object> count = new ArrayList<Object>();
List<Object> date = new ArrayList<Object>();
try {
jdbcTemplate.query(query, new Object[] { priority }, new RowMapper<Object>() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
date.add(rs.getString("date"));
count.add(rs.getInt("count"));
return null;
}
});
} catch (DataAccessException e) {
e.printStackTrace();
}
return map;
}
如何在查询中赋予一次优先级并使用两次?
解决方案
select t1.date,count(*) from table1 t1
where (DATE_PART('day', current_date::timestamp - t1.date::timestamp ))
<(SELECT value from keyvaluepair k where k.priority=t1.priority)
and t1.priority = 'Important' group by t1.date
order by t1.date
查询中的这种更改帮助我解决了我遇到的错误,如图所示 No value specified for parameter 2.; 嵌套异常是 org.postgresql.util.PSQLException: No value specified for parameter 2. org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; 其中表的两个列名相同,我必须为所有参数传递一次值,如下面的链接所示
http://localhost:8080/get/getDates?priority=Important
select t1.date,count(*) from table1 t1
where (DATE_PART('day', current_date::timestamp - t1.date::timestamp ))
<(SELECT value from keyvaluepair k where k.priority=t1.priority)
and t1.priority = ? group by t1.date
order by t1.date
在此链接中,重要的单个值用于查询它用于相同列名的两个表参数的位置。
推荐阅读
- pandas - 熊猫数据框到字符串操作
- python - 从圣杯调用另一个 lambda
- angular - (node:6608) UnhandledPromiseRejectionWarning: 错误: spawn EPERM
- angular - 上传 png 文件的单元测试
- javascript - 输入范围滑块左侧的颜色与 React with ref.current.style 中滑块右侧的颜色不同?
- r - 绘制参考值 = 0 的泊松模型的预测
- google-sheets - 在 Google 文档中更改角色的机会
- javascript - TypeError:无法读取发送的属性(Discord.js)
- sql-server - 每小时将表复制到另一个表而不删除的事件
- c++ - 我应该在 FindFirstFile 函数中进行哪些更改以使其显示完整的文件名?