我有一个前后端分离的项目,在后端我有两个查询函数,一个是查询表中有哪几个年份,一个是把上一个查出来的年份作为参数查出所在年份有几个人
查询有哪几个年份
@Select(" SELECT DISTINCT DATE_FORMAT(DATE,'%Y')AS DATE\n" +
" FROM student\n" +
" ORDER BY DATE")
List<Integer> getYear();
结果:[2018, 2019, 2020, 2021, 2022]
另一个查询函数:Integer getYearCount(Integer year);
如何把这个结果作为参数编写SQL语句
在Mybatis中
比如(查询语句的参数哪里不知道填什么):
<select id="getYearCount" resultType="int" parameterType="int">
SELECT COUNT(student.id) FROM student WHERE DATE LIKE('%#{}%')
</select>
不知道参数该如何填
// 第一步:
getYearCount有参数,为List<Integer>,例如声明为List<Integer> getYearCount(List<Integer> years);
// 查询语句中,用法如下:
<select id="getYearCount" resultType="java.lang.Integer">
SELECT COUNT(student.id) FROM student
<foreach item="year" collection="list" separator="or" open="WHERE" close="">
DATE LIKE concat('%',#{year},'%')
</foreach>
group by date
</select>