1. 程式人生 > >sql雙重for迴圈

sql雙重for迴圈

<!-- 雙重for迴圈測試 -->
		<select id="selectUserInfoByIds" parameterType="java.lang.String"	resultMap="users">
	
	<foreach collection="ids" item="item" separator="union all">
		select
		
		ID,
		IDTYPE,
		IDNUM,
		USERNAME,
		PHONENUM,
		CREATETIME, 
		LASTMODIFIED,
		EXT1,
		EXT2,
		EXT3
		
		from (
			<foreach collection="userTables" item="item" separator="union all">
		
			select
			ID,
			ID_TYPE IDTYPE,
			ID_NUM IDNUM,
			USER_NAME USERNAME,
			PHONE_NUM PHONENUM,	
			to_char(CREATE_TIME,'yyyy-mm-dd hh24:mi:ss') CREATETIME,
			to_char(LAST_MODIFIED,'yyyy-mm-dd hh24:mi:ss') LASTMODIFIED,
			EXT1,
			EXT2,
			EXT3
			from ${item}
			where 1=1
			
			<if test="id  != null and id != ''">
			   and ID=#{id}
			</if>
			
	</foreach>
		)
		where 1=1
	</foreach>
		order by ID asc
	</select>