sql语句中,使用case when实现判断
select
(case when 判断字段表达式 then 值1 else 值2 end) as 别名
from 表
where 条件
比如:
<select id="selectByAccountPhone" resultType="cn.group.user.model.Account">
SELECT
a.account_no AS accountNo,
a.account_name AS accountName,
a.account_email AS accountEmail,
a.account_phone AS accountPhone,
b.account_status AS accountStatus,
a.account_real_name AS accountRealName,
(CASE WHEN a.account_company_name = '' THEN 1 ELSE 2 END ) AS userType
from tb_account as a, tb_account_app as b
<where>
a.account_no = b.account_no
<if test="accountPhone != null and accountPhone != ''"> and a.account_phone = #{accountPhone} </if>
</where>
order by a.account_update desc
</select>