mysql - <choose>多条件判断 (等同于switch,case,default)

使用介绍

choose 标签作用是通过条件判断来拼接 SQL 语句,类似于 Java 中的 switch 语句,从上到下,当有匹配的条件时,跳出 choose 语句;如果所有条件都不成立则执行 otherwise 标签中的内容

注意最先需要判断的条件要放在前面,当匹配到某个条件时,就不会判断后面的语句了。

语法

<choose>
	<when test=条件1>
		...
	</when>
	<when test=条件2>
		...
	</when>
	<when test=条件3>
		...
	</when>
	...
	<otherwise>
		...
	</otherwise>
</choose>

<select id="selectDistributeUserIds" resultType="java.lang.Integer">
	select u.id from users u
	<where>
		u.belongDistributorUserId is not null
		<if test="registerStart!=null">and #{registerStart} &lt;= u.createdAt</if>
		<if test="registerEnd!=null">and #{registerEnd} >= u.createdAt</if>
		<if test="id!=null">and u.id=#{id}</if>
		<if test="status!=null">and u.status=#{status}</if>
		<if test="username!=null and username!=''">and u.username like concat('%',#{username},'%')</if>
		<if test="email!=null and email!=''">and u.email like concat('%',#{email},'%')</if>
		<if test="phone!=null and phone!=''">and u.phone like concat('%',#{phone},'%')</if>
		<if test="remark!=null and remark!=''">and u.remark like concat('%',#{remark},'%')</if>
		<if test="distributorUrl!=null and distributorUrl!=''">and u.distributorUrl =#{distributorUrl}</if>
		<if test="belongCashoutStatus!=null">and u.belongCashoutStatus=#{belongCashoutStatus}</if>
		<choose>
			<when test="distributorLevel!=null">
				<if test="distributorLevel==1">
					<if test="belongFirstUserId!=null">and u.belongFirstUserId=#{belongFirstUserId} and u.belongSecondUserId is not null</if>
				</if>
				<if test="distributorLevel==2">
					<if test="belongSecondUserId!=null">and u.belongSecondUserId=#{belongSecondUserId} and u.belongThirdUserId is not null</if>
				</if>
			</when>
			<otherwise>
				<if test="belongFirstUserId!=null">and u.belongFirstUserId=#{belongFirstUserId}</if>
				<if test="belongSecondUserId!=null">and u.belongSecondUserId=#{belongSecondUserId}</if>
				<if test="belongThirdUserId!=null">and u.belongThirdUserId=#{belongThirdUserId}</if>
			</otherwise>
		</choose>
	</where>
	order by u.createdAt desc
</select>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 这个命令是用来在命令行中以指定的用户名、主机和密码登录到MySQL服务器的。其中,-u参数后面跟的是用户名,-h参数后面跟的是主机名或IP地址,-p参数后面跟的是密码。所以,你需要将<User>替换为你的用户名,<IP>替换为你要连接的主机的IP地址,<Password>替换为你的密码。例如,如果你的用户名是root,要连接的主机IP地址是192.168.0.1,密码是123456,那么命令就应该是mysql -uroot -h192.168.0.1 -p123456。这样就可以使用指定的用户名、主机和密码登录到MySQL服务器了。\[1\] #### 引用[.reference_title] - *1* [cmd连接mysql连接:mysql-h主机地址-u用户名-p用户密码(注:u与root可以不用加)](https://blog.csdn.net/weixin_39831991/article/details/113151865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [MySQL介绍](https://blog.csdn.net/qq_52262831/article/details/122589445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [输入命令mysql -u root -p 报错](https://blog.csdn.net/weixin_29333353/article/details/113138220)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yixian123.com

谢谢打赏,祝老板心想事成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值