mysql where条件中的判断语句

mysql where条件中的判断语句 

<if test="categoryId != null">
AND CASE 1 WHEN "Y"=(SELECT is_open from category as oca where oca.category_id=#{categoryId,jdbcType=VARCHAR}) 
THEN (cat.category_id = #{categoryId,jdbcType=VARCHAR} OR cat.parent_id = #{categoryId,jdbcType=VARCHAR}) 
ELSE (cat.category_id = #{categoryId,jdbcType=VARCHAR}) 
END
</if>

mybatis的配置文件:

	<!-- 根据条件分页查询媒体文件信息(用于栏目专题)  -->
	<select id="selectMedias" parameterType="map" resultType="com.school.domain.MediaBean">
		SELECT c.course_id AS courseId, c.category_id AS categoryId, c.school_id AS schoolId,
			c.course_type AS courseType, c.title, c.subtitle,c.summary, c.keywords, c.objectives, c.audiences, c.requirements,
			c.cover_image AS coverImage, c.promo_video_id AS promoVideoId, c.promo_video_url AS promoVideoUrl,
			c.privacy, c.passwd, c.is_free AS isFree,c. price, c.validity, c.user_id AS userId, c.status,
			c.is_recommended AS isRecommended, c.date_recommended AS dateRecommended,c.is_recommended_cetagory AS isRecommendedCetagory,
			c.ce_date_recommended AS ceDateRecommended, c.index_order AS indexOrder,c.special_order AS specialOrder, c.date_added AS dateAdded, c.last_modified AS lastModified,
			c.published_start_millis AS publishedStartMillis, c.published_end_millis AS publishedEndMillis, c.duration,
			u.nickname, u.avatar, cat.name As categoryName
		FROM course AS c
		LEFT JOIN user AS u ON(c.user_id = u.user_id)
		LEFT JOIN category AS cat ON(cat.category_id = c.category_id)
		WHERE c.school_id="wxj" 
		<if test="categoryId != null">
			 AND CASE 1 WHEN "Y"=(SELECT is_open from category as oca where oca.category_id=#{categoryId,jdbcType=VARCHAR}) 
			 THEN (cat.category_id = #{categoryId,jdbcType=VARCHAR} OR cat.parent_id = #{categoryId,jdbcType=VARCHAR}) 
			 ELSE (cat.category_id = #{categoryId,jdbcType=VARCHAR}) 
			 END
		</if>
		<if test="courseType != null">
		    AND c.course_type=#{courseType,jdbcType=VARCHAR}
		</if>
		<if test="currTimeMillis != null">
		    <![CDATA[
		             AND c.published_start_millis <= ${currTimeMillis} AND c.published_end_millis >= ${currTimeMillis}
		    ]]>
		</if>
		<if test="orderByClause != null">
		    ORDER BY ${orderByClause}
		</if>
		<if test="limitClause != null">
		    LIMIT ${limitClause}
		</if>
	</select>


你可能感兴趣的:(mysql where条件中的判断语句)