出现java.sql.SQLException: Subquery returns more than 1 row错误的原因,解决方法

  • Post author:
  • Post category:java


今天在使用嵌套查询语句时出现这个错误,也很好解决,在这里记录一下。

例如这个语句:

<select id="findSongBySheetId" resultType="com.jhb.crash_music.pojo.Song" parameterType="int">
        select * from song_table
        where song_id =(select songlist_songId from songlist_table where songlist_sheetId=#{sheet_id})
</select>

我是想通过多个sheet_id去查询一个song_id列表,然后通过song_id这个列表再来查询相应的song,结果出现报错:java.sql.SQLException: Subquery returns more than 1 row,查看报错信息显示我查询出了超过一行的行数。

因为当用where song_id=**时,这个等于对应的只能是一组,当你想多组嵌套查询时,需要将=改成in:

<select id="findSongBySheetId" resultType="com.jhb.crash_music.pojo.Song" parameterType="int">
        select * from song_table
        where song_id in (select songlist_songId from songlist_table where songlist_sheetId=#{sheet_id})
</select>

之后进行运行,没有报错,查询结果也正常。

总结:

在一组查询条件时,用等于 = ;

在多组查询条件列表时,用 in ;



版权声明:本文为qq_46418539原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。