关于Mysql语句的相关考题(1)
题目相关图表
题目问题
1,查找小于26岁的用户信息,展示用户的基本信息(昵称、邮箱、性别、年龄);
2,页面需要展示lilei用户的基本信息(昵称、邮箱、性别、年龄、爱好),请设计相关sql;
3,页面需要展示xiaoqiang用户关于篮球爱好的好友推荐信息,展示内容包含推荐好友的基本信息(昵称、年龄、性别、邮箱、爱好),并按年龄倒序排序,请设计相关sql;
相关解答
1,第一题应该比较简单,是MySQL的基本用法
2,有2种解答,但第二种更好
解法一:select p.nickname,p.email,p.sex,p.age,group_concat(distinct h.hobby_value)
from user_property as p
left join hobby_dict as h
on find_in_set(h.hobby_key,p.hobbies)
where p.usename=‘tengfei’ or p.usename=‘lilei’;
解法二:select * from user_property as P
left join hobby_dict as H
on P.hobbies like concat(’%’,h.hobby_key,’%’)
where p.usename=‘tengfei’ or p.usename=‘lilei’ order by P.nickname
3,有2种思路
思路一:#先连接 关系表, 再过滤 篮球
select * from user_property as P
inner join user_friend_relation as F
on P.usename = F.friend_id and F.usename=‘xiaoqiang’
inner join hobby_dict as H
on P.hobbies like concat(’%’,h.hobby_key,’%’) and h.hobby_value=‘篮球’;
思路二:#先过滤 篮球 ,查 关系表,
select * from user_property as P
inner join hobby_dict as H
on P.hobbies like concat(’%’,h.hobby_key,’%’) and h.hobby_value=‘篮球’
where P.usename
in (select friend_id from user_friend_relation where usename=‘xiaoqiang’);
大家还有其他解法,欢迎一起谈论!