MySQL开发技巧(三)3-4 [MySQL] 使用关联方式实现多属性查询(二)

本文深入解析了一段复杂的SQL查询语句,该语句通过多个LEFT JOIN操作从'user1'表中选择具有特定技能的用户,并使用CASE WHEN语句计算用户拥有的技能数量,最后筛选出至少具备两项技能的用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Select a.user_name,b.skill,c.skill,e.skill From user1 a
From user1 a 
Left join user1_skill b on a.id =b.user_id and b.skill='念经' and
b.skill level>0
Left join user1_skill c on a.id = c.user_id and c.skill='变化' and
c.skill_level>0
Left join user1_skill d on a.id = d.user_id and d.skill = '腾云' and
c.skill_level>0
Left join user1_skill e on a.id = e.user_id and e.skill = '浮水' and
e.skill_level>0
;
use test
Where (case when b.skill is not null then 1 else 0 end)
+(case when c.skill is not null then 1 else 0 end)
+(case when d.skill is not null then 1 else 0 end)
+(case when e.skill is not null then 1 else 0 end)>=2;

转载于:https://my.oschina.net/u/3430557/blog/1028370

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值