当前位置: 首页 > news >正文

【SQL语句】SQL编写规范

简介

本文编写原因主要来于XC迁移过程中修改SQL语句时,发现大部分修改均源自于项目SQL编写不规范,以此文档做以总结。

  • 注:此文档覆盖不甚全面,大体只围绕迁移遇到的修改而展开。

正文

1、【字段引号】
列名、表名如无特殊情况,不要添加任何引号(` ’ 等)。若返回列名有可能是关键字,例如month、year等,必须使用双引号包裹。

# 错误
SELECT book_name as bookMame, year as year;
# 正确
SELECT book_name as bookMame, year as "year";

2、【除0兼容】
存在除法计算的时候(/),要确保除数为0的兼容。MYSQL一定程度上兼容这种计算,但是对于其他数据库则不一定,最好的办法是使用函数nullif对被除数进行零值判断。

# 错误
SELECT a/b
# 正确
select a/nullif(b, 0)函数解释:
NULLIF(表达式1,表达式2)
NULLIF函数比较表达式1和表达式2。如果两个表达式相等就返回空值(NULL)。如果不等就返回表达式1。

3、【严格分组】
在对表进行聚合查询的时候,只能在 SELECT 子句中写下面 3 种内容:通过 GROUP BY 子句指定的聚合键、聚合函数(SUM 、AVG 等)、常量。但通常可以通过设置数据库的sql_mode,移除only_full_group_by解决此问题,但是若无特殊情况,还是应保障遵守严格模式标准。若能保证某个字段在业务中每个分组中只会有一个值,并且也不想用作group by字段语句,可通过max(特殊字段)方式达到类似的效果。

# 不规范写法
SELECT a, b
from tb
group by a;# 兼容写法
SELECT a, max(b)
from tb
group by a;# 规范写法,既然b没有参与分组,就不该出现在select子句中
SELECT a
from tb
group by a;

4、【类型区别】
日期不要和字符串类型进行比较

update safety_manage_organ 
set spread_certificate_status = if(spread_end_time is null || spread_end_time >= now(), '有效', '无效'), 
chief_engineer_certificate_status = if(chief_engineer_end_time is null || chief_engineer_end_time >= now(), '有效', '无效'), deputy_safe_certificate_status = if(deputy_safe_end_time is null || deputy_safe_end_time >= now(), '有效', '无效'), electric_certificate_status = if(electric_end_time is null || electric_end_time >= now(), '有效', '无效'), 
produce_certificate_status = if(produce_end_time is null || produce_end_time >= now(), '有效', '无效')
WHERE enterprise_id = 260;update safety_manage_organ 
set spread_certificate_status = if(spread_end_time is null || spread_end_time >= date_format(now(), '%Y-%m-%d'), '有效', '无效')
WHERE enterprise_id = 260;

5、【union规范】
union 连接的所有数据行要保证列顺序一致且字段类型相同。

# 错误写法
select 0 entId, 0 province, 0 city, 0 district from dual
union select '10164564' entId, '520000' province, '522000' city, '522022' district from dual# 正确写法
select '0' entId, '0' province, '0' city, '0' district from dual
union select '10164564' entId, '520000' province, '522000' city, '522022' district from dual

6、【多表关联】
多表关联时,最好为每张表指定别名,第一复杂查询时方便定位,第二某些数据库会报字段不明确的错误情况。

# 错误写法: 字段关联 "industry_type" 是不明确的
SELECT A.industry_type AS industry_type,SUM ( A.predict_count ) AS predict_count,( SUM ( A.predict_count ) - SUM ( b.real_count ) ) AS predict_change_value,(ROUND(( ( SUM ( A.predict_count ) - SUM ( b.real_count ) ) / SUM ( b.real_count ) ) * 100,2 ) ) AS predict_change_rate,SUM ( error_count ) AS error_count 
FROM( SELECT SUM ( predict_count ) AS predict_count, error_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' GROUP BY industry_type )AS A JOIN ( SELECT real_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' - 1 GROUP BY industry_type ) AS b ON A.industry_type = b.industry_type 
GROUP BY industry_type;# 正确写法,group by 跟上A.industry_type
SELECT A.industry_type AS industry_type,SUM ( A.predict_count ) AS predict_count,( SUM ( A.predict_count ) - SUM ( b.real_count ) ) AS predict_change_value,(ROUND(( ( SUM ( A.predict_count ) - SUM ( b.real_count ) ) / SUM ( b.real_count ) ) * 100,2 ) ) AS predict_change_rate,SUM ( error_count ) AS error_count 
FROM( SELECT SUM ( predict_count ) AS predict_count, error_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' GROUP BY industry_type )AS A JOIN ( SELECT real_count, industry_type FROM electricity_produce_predict_info WHERE YEAR = '2022' - 1 GROUP BY industry_type ) AS b ON A.industry_type = b.industry_type 
GROUP BY A.industry_type;

结语

本文暂时列举此几大类,其主要应用于PG数据库,但也可代表所有,因为PG几乎是所有数据库中要求比较严格的一种。

http://www.lryc.cn/news/137890.html

相关文章:

  • 后端项目开发:工具类封装(序列化、反射)
  • 软件测试技术分享丨遇到bug怎么分析?
  • LeetCode无重复字符的最长子串
  • 17.2.2 【Linux】通过systemctl观察系统上所有的服务
  • Redis扩容机制与一致性哈希算法解析
  • BDA初级分析——可视化基础
  • 边缘计算节点BEC典型实践:如何快速上手PC-Farm服务器?
  • python自动把内容发表到wordpress完整示例及错误解答
  • 【javaweb】学习日记Day6 - Mysql 数据库 DDL DML DQL
  • 如何利用SFTP如何实现更安全的远程文件传输 ——【内网穿透】
  • 枚举和反射
  • MinIO【部署 01】MinIO安装及SpringBoot集成简单测试
  • 问道管理:证券代码是什么?有什么用?
  • 中文医学知识语言模型:BenTsao
  • Java基础十四(字符串)
  • vue3 基础知识 (动态组件 keep-alive 等) 04
  • 【C# Programming】编程入门:数组、操作符、控制流
  • 线上问诊:业务数据采集
  • 2023 CCPC 华为云计算挑战赛 hdu7399 博弈,启动!(图上博弈/枚举+逆向有向图sg函数)
  • Unity之 Vector3 的详细介绍以及方法的介绍
  • Postgresql部署及简单操作
  • rabbitmq集群搭建
  • C++ 二叉搜索树的概念特性
  • 7、Spring_AOP
  • QChart:数据可视化(用图像形式显示数据内容)
  • 【python】Leetcode(primer-set)
  • 【LVS集群】
  • 软考高级系统架构设计师系列之:论文题目类型、论文考试大纲、历年考试论文真题汇总、论文写作原则、论文写作常见问题、论文评分标准
  • 完整的application.xml
  • C语言:运算符优先级