SSM项目xml中的一条select语句,包含大于小于号、日期比较及存在于一个表不存在于另一个表
xml中的一条涉及多问题的select语句
数据库用的是MySql
下面这条语句涉及到(1).xml文件中大于小于号的表示。(2)日期的比较。(3)存在于一个表且不存在于另一个表。三个问题的解决办法。
<select id="getApplyList" parameterType="com.xxx.platform.entity.ApplyMatingCustom" resultType="com.xxx.platform.entity.ApplyMatingCustom">
SELECT * FROM
(
SELECT
apply_mating.apply_mating_id,
apply_mating.apply_num,
apply_mating.apply_status,
apply_mating.apply_appointment_time,
apply_mating.add_time,
apply_mating.horse_id,
apply_mating.male_stallion_horse_id,
user.user_tel,
user.user_name
FROM
apply_mating
LEFT JOIN user ON user.user_id = apply_mating.apply_user_id
LEFT JOIN birth_record ON apply_mating.apply_mating_id = birth_record.apply_mating_id
WHERE
apply_mating.staff_breeder_id =#{staff_breeder_id}
<if test="is_over == 0">
AND apply_mating.apply_status < 5
</if>
<if test="is_over == 1">
AND apply_mating.apply_status > 4
<if test="is_registered == 0">
AND birth_record.apply_mating_id IS NULL
</if>
<if test="is_registered == 1">
AND birth_record.apply_mating_id IS NOT NULL
</if>
<if test="horse_id != null">
AND apply_mating.horse_id =#{horse_id}
</if>
</if>
<if test="start_date != null">
AND Date(apply_mating.add_time) <![CDATA[>=]]> #{start_date}
</if>
<if test="end_date != null">
AND Date(apply_mating.add_time) <![CDATA[<=]]> #{end_date}
</if>
GROUP BY apply_mating.apply_mating_id
ORDER BY apply_appointment_time
)
AS RESULT LIMIT
#{page.pageStart},#{page.pageSize}
</select>
1、.xml文件中大于小于号的表示1
(1)用转义字符,如上面代码中
申请状态 < 5的
AND apply_mating.apply_status < 5
表示 | 符号 | 说明 |
---|---|---|
<; | < | 小于号 |
>; | > | 大于号 |
(2)使用 < ![ CDATA[ ] ] > 符号,如上面代码中
添加日期>=start_date的
AND Date(apply_mating.add_time) <![CDATA[>=]]> #{start_date}
2、日期比较大小2
MySql中字段类型为datetime的话,数据格式为 yyyy-MM-dd HH:mm:ss。此时,如果只想比较日期的话,可使用Date()函数来返回日期部分,如:
select * from product where Date(add_time) = '2019-01-01'
对于上面代码来说,有:
AND Date(apply_mating.add_time) <![CDATA[<=]]> #{end_date}
注意这里的end_date的格式
3、存在于一个表不存在于另一个表3
select A.ID from A left join B on A.ID=B.ID where B.ID is null
参考: