我正在尝试使用Postgresql的unnest函数进行多次插入,以列出@krokodilko这样的tip数据。
@Modifying(clearAutomatically = true)
@Query(value="insert into training_exercise(training_id, exercise_id) SELECT ?1, x FROM unnest(ARRAY?2) x ",nativeQuery=true)
void addExercises(Long id, List<Long> exercisesIds);
问题是产生的sql类似于:
insert into training_exercise(training_id, exercise_id) SELECT 1, x FROM unnest(ARRAY(1,2,3)) x
这给了我一个错误,原因是要使用数组,必须使用“ [”而不是“(”。
我找到了一种解决方法,将查询修改为:
insert into training_exercise(training_id, exercise_id) SELECT ?1, exercises.id FROM (select exercises.id from exercises where exercises.id in ?2) as exercises.
有什么办法可以使我的初衷达到目的?