长sql写法总结

如果各位供职于操纵数据库较多的公司,那么一定避免不了写长sql,会写长sql是程序员的一项默认技能,最近我在研究长sql的写法,首先一条sql根据需求的不同,首先要分析,需求的内容,一般写长sql,都是从最里面开始写,要牢记,sql的作用就是搂数据,怎么能搂出来,就怎么用,sql语句的固定格式永远都是《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》,所谓的长sql就是在在这个基本的框架下不停的嵌套和叠加,还有替换,比如我们在分析需求的时候,首先可能你所需要搂数据的表就是通过很多表所连接而成的,那么你就先构造出你需要的表,这就涉及到“替换”,刚才公式《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》其中表名就可以构造,构造方法即《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》,然后你想要搂出的数据是由其他几个表的某些字段组成,同样也可以《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》,这样就变成了长sql:
《select + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》+ from + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》 + where + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》》甚至子查询中还可以嵌套
比如:
《select + 《select + 你想要搂出来的数据 + from +《select + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》+ from + 《select + 你想要搂出来的数据 + from + 《select + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》+ from + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》 + where + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》》 + where + 限制条件》 + where + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》》 + where + 限制条件》+ from + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》 + where + 《select + 你想要搂出来的数据 + from + 表名 + where + 限制条件》》只要你自己不晕,这样肯定最后能取到想要的数据,前提是数据表是正确的。
这就是一个简单的例子

发布了13 篇原创文章 · 获赞 4 · 访问量 3213

猜你喜欢

转载自blog.csdn.net/leadder/article/details/93916909