2017-07-20 4 views
0

特定の月と年の組み合わせに一致するレコードを検索したい。mybatisでforeachで2つのリストを同時に反復する

私は月数3を指定すると、クエリはこれらのように過去3か月間かかるでしょう。

選択*私はのリストを持っている(月= 8月と年= 2017)または(月= 7月と年= 2017)または(月= 6月と年= 2017)

<select id="getAll" parameterType="list" resultType="test"> 
Select * from table where  
<foreach item="month" index="key" collection="months" separator=" OR " open="(" close=")"> 
      month = #{month} AND year = #{years[ (${key}) ]} 
     </foreach> 
</select> 

テーブル から月{8月、7月、6月}と年のリスト{2017,2017,2017}。

mybatisのforeachを使用して上記の結果を達成するにはどうすればよいですか?

答えて

0

私はこの例を再現できませんが、ネストした 'each for'要素を実装することは可能でしょうか?

<select id="getOred" parameterType="list" resultType="test"> 
Select * from table where 
    <foreach item="month" index="mkey" collection="months" open="(" separator=" OR " close=")"> 
     <foreach item="year" index="ykey" collection="years" open="" separator="" close=""> 
      <if test="mkey == ykey"> 
       month = #{month} AND year = #{year} 
      </if> 
     </foreach> 
    </foreach> 
関連する問題