初学者MySQL语句笔记
我是一名Android开发者,今年年初来公司入手H5与后台,首先接触的是数据接口。
首先献丑拿出我写的第一条蠢的不能再蠢的SQL语句(不要笑话)
需求:根据相册id找出评论内容数据
select sc.*,
(select fu.nick_name from f_user fu where sc.user_id = fu.id)as user_name,
(select fu.head_img from f_user fu where sc.user_id = fu.id)as user_img,
(select fu.nick_name from f_user fu where sc.target_id = fu.id)as target_name,
(select fu.head_img from f_user fu where sc.target_id = fu.id)as target_img
from s_topic_comment st,s_comment_reply sc
where sc.comment_id = st.id and st.type=2 and st.topic_id=0
and st.id ='"+id+"'
这里错误: 1.怕表名 重名 我用两个字母来代替表名
2.多次嵌套select语句
再后来好好看了一个MySQL的基础教程之后:
select c.*,f1.nick_name,f1.head_img,f2.nick_name,f2.head_img
from s_topic_comment t, s_comment_reply c
left join f_user f1 on f1.id= c.user_id
left join f_user f2 on f2.id= c.target_id
where c.comment_id = t.id and t.type=2 and t.topic_id=0
and t.id ='"+id+"'
总结:以为达到效果就可以了, 没想到这是大忌, 这样的语句 别人看着就是受罪。
后续还会补充...
浙公网安备 33010602011771号