SQL知识点合集---第二弹

数据一 

 <select id="listPositionAuditCheckSample" resultType="net.nxe.cloud.content.server.entity.PositionAuditCheckSample"><trim prefixOverrides="union all"><if test="userSampleCount != null and userSampleCount > 0">select pat.position_id position_id, pat.id audit_idfrom (select t.creator,cast(concat('[',substring_index(group_concat(concat('"', t.audit_id, '"') order by rand()),',',#{userSampleCount}),']') as json) audit_idsfrom (select pat.position_id, max(pat.id) audit_id, pat.creatorfrom position_audit patwhere pat.creator > 1and pat.entry in ('AUDITING', 'RECHECKING')and pat.create_time between #{startDate} and date_add(#{endDate}, interval 86399 second)and pat.creator in (<foreach collection="auditors" item="auditor" separator=",">#{auditor}</foreach>)and pat.finished = 1 and pat.id = pat.`group`group by pat.position_id, pat.creator, date_format(pat.create_time, '%Y-%m-%d')) tinner join position pn on pn.id = t.position_id and pn.updater = t.creator and pn.source = 'USER'group by t.creator) tinner join position_audit pat on pat.creator = t.creatorand json_contains(t.audit_ids, concat('"', pat.id, '"'))</if><if test="captureExactSampleCount != null and captureExactSampleCount > 0">union allselect pat.position_id position_id, pat.id audit_idfrom (select t.creator,cast(concat('[',substring_index(group_concat(concat('"', t.audit_id, '"') order by rand()),',',#{captureExactSampleCount}),']') as json) audit_idsfrom (select pat.position_id, max(pat.id) audit_id, pat.creatorfrom position_audit patwhere pat.creator > 1and pat.entry in ('AUDITING', 'RECHECKING')and pat.create_time between #{startDate} and date_add(#{endDate}, interval 86399 second)and pat.creator in (<foreach collection="auditors" item="auditor" separator=",">#{auditor}</foreach>)and pat.finished = 1 and pat.id = pat.`group`group by pat.position_id, pat.creator, date_format(pat.create_time, '%Y-%m-%d')) tinner join position pnon pn.id = t.position_id and pn.updater = t.creator and pn.source = 'CAPTURED' and `vague` = 0group by t.creator) tinner join position_audit pat on pat.creator = t.creatorand json_contains(t.audit_ids, concat('"', pat.id, '"'))</if><if test="captureVagueSampleCount != null and captureVagueSampleCount > 0">union allselect pat.position_id position_id, pat.id audit_idfrom (select t.creator,cast(concat('[',substring_index(group_concat(concat('"', t.audit_id, '"') order by rand()),',',#{captureVagueSampleCount}),']') as json) audit_idsfrom (select pat.position_id, max(pat.id) audit_id, pat.creatorfrom position_audit patwhere pat.creator > 1and pat.entry in ('AUDITING', 'RECHECKING')and pat.create_time between #{startDate} and date_add(#{endDate}, interval 86399 second)and pat.creator in (<foreach collection="auditors" item="auditor" separator=",">#{auditor}</foreach>)and pat.finished = 1 and pat.id = pat.`group`group by pat.position_id, pat.creator, date_format(pat.create_time, '%Y-%m-%d')) tinner join position pnon pn.id = t.position_id and pn.updater = t.creator and pn.source = 'CAPTURED' and `vague` = 1group by t.creator) tinner join position_audit pat on pat.creator = t.creatorand json_contains(t.audit_ids, concat('"', pat.id, '"'))</if></trim></select>

知识点 

<trim>标签的使用

  • 作用:去除 SQL 片段开头多余的 UNION ALL

  • 示例

<trim prefixOverrides="union all"><if>...</if><if>...</if>
</trim>
  • 说明:如果第一个 <if> 条件不满足,生成的 SQL 不会以 UNION ALL 开头,避免语法错误。

GROUP_CONCAT + ORDER BY RAND()

  • 作用:将多个值拼接成字符串,并按随机顺序排序。

  • 示例

group_concat(concat('"', t.audit_id, '"') order by rand())

SUBSTRING_INDEX

  • 作用:截取字符串的前 N 个元素。

  • 示例

substring_index(group_concat(...), ',', #{userSampleCount})

CAST(... AS JSON)

  • 作用:将字符串转换为 MySQL 的 JSON 类型。

  • 示例

cast(concat('[', substring_index(...), ']') as json)

JSON_CONTAIN

  • 作用:检查 JSON 数组中是否包含某个值。

  • 示例

json_contains(t.audit_ids, concat('"', pat.id, '"'))

JSON数据类型处理

函数作用示例场景
JSON_ARRAY()创建 JSON 数组初始化订单状态历史
JSON_ARRAY_APPEND()向数组追加元素添加新状态
JSON_LENGTH()获取数组/对象长度限制状态历史长度
JSON_MERGE_PATCH()合并并覆盖重复键(对象)更新物流信息
JSON_EXTRACT()提取 JSON 中的值查询最新状态
JSON_CONTAINS_PATH()检查 JSON 中是否存在指定路径验证物流信息是否存在
JSON_MERGE()合并 JSON 文档(兼容旧版本)合并用户备注(注意数组行为)
CREATE TABLE orders (order_id INT PRIMARY KEY AUTO_INCREMENT,user_id INT NOT NULL,status_history JSON COMMENT '状态变更历史(JSON数组)',extra_info JSON COMMENT '额外信息(JSON对象)'
);-- JSON_ARRAY()、JSON_OBJECT
INSERT INTO orders (user_id, status_history, extra_info)
VALUES (1001,JSON_ARRAY('created'),  JSON_OBJECT('note', '请尽快发货') 
);-- JSON_ARRAY_APPEND:追加数据
UPDATE orders
SET status_history = JSON_ARRAY_APPEND(status_history, '$', 'ios')
WHERE order_id = 1;-- JSON_MERGE_PATCH:追加数据
UPDATE orders
SET extra_info = JSON_MERGE_PATCH(extra_info,'{"logistics": {"company": "顺丰", "tracking_no": "SF123456"}}'
)
WHERE order_id = 1;-- JSON_EXTRACT() 提取最后一个状态
SELECTJSON_EXTRACT(status_history, '$[last]') AS last_status
FROM orders
WHERE order_id = 1;-- JSON_CONTAINS_PATH() 验证extra_info中是否有物流信息
SELECTJSON_CONTAINS_PATH(extra_info, 'one', '$.logistics') AS has_logistics
FROM orders
WHERE order_id = 1;-- JSON_MERGE:会合并为数组
UPDATE orders
SET extra_info = JSON_MERGE(extra_info,'{"note": "已加急处理"}'
)
WHERE order_id = 1;-- JSON_LENGTH()长度
SELECT * from orders where JSON_LENGTH(status_history)>2

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/77580.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【QT】QT控制硬件

QT控制硬件 1.上位机程序开发2.具体例子控制led灯3. linux中的函数跟QT类里面的函数同名&#xff0c;发生冲突4.示例代码 1.上位机程序开发 QT做一个上位机程序&#xff0c;控制底层的硬件设备(下位机) 总结&#xff1a; 在构造函数里面去初始化&#xff0c;打开硬件驱动在析…

Flutter介绍、Flutter Windows Android 环境搭建 真机调试

目录 Flutter介绍 Windows 环境搭建 1.安装配置JDK 2.下载安装Android Studio 3.下载配置Flutter SDK ​4.运行Flutter doctor命令检测环境是否配置成功 ​5.打开Android Studio安装Flutter/Dart 插件 ​6.插件运行Flutter项目 ​编辑 Flutter Android真机调试 Flut…

Android Studio 中使用 SQLite 数据库开发完整指南(Kotlin版本)

文章目录 1. 项目准备1.1 创建新项目1.2 添加必要依赖 2. 数据库设计3. 实现数据库3.1 创建实体类 (Entity)3.2 创建数据访问对象 (DAO)3.3 创建数据库类 4. 创建 Repository5. 创建 ViewModel6. 实现 UI 层6.1 创建笔记列表 Activityactivity_notes_list.xmlNotesListActivity…

Vue基础(7)_计算属性

计算属性(computed) 一、使用方式&#xff1a; 1.定义计算属性&#xff1a; 在Vue组件中&#xff0c;通过在 computed 对象中定义计算属性名称及对应的计算函数来创建计算属性。计算函数会返回计算属性的值。 2.在模板中使用计算属性&#xff1a; 在Vue的模板中&#xff0c;您…

辛格迪客户案例 | 华道生物细胞治疗生产及追溯项目(CGTS)

01 华道&#xff08;上海&#xff09;生物医药有限公司&#xff1a;细胞治疗领域的创新先锋 华道&#xff08;上海&#xff09;生物医药有限公司&#xff08;以下简称“华道生物”&#xff09;是一家专注于细胞治疗技术研发与应用的创新型企业&#xff0c;尤其在CAR-T细胞免疫…

[26] cuda 应用之 nppi 实现图像格式转换

[26] cuda 应用之 nppi 实现图像格式转换 讲述 nppi 接口定义通过nppi实现 bayer 格式转rgb格式官网参考信息:http://gwmodel.whu.edu.cn/docs/CUDA/npp/group__image__color__debayer.html#details1. 接口定义 官网关于转换的原理是这么写的: Grayscale Color Filter Array …

2025“钉耙编程”中国大学生算法设计春季联赛(8)10031007

题目的意思很好理解找从最左边到最右边最短路&#xff08;BFS&#xff09; #include <bits/stdc.h> using namespace std; int a[510][510]; // 存储网格中每个位置是否有障碍&#xff08;1表示有障碍&#xff0c;0表示无障碍&#xff09; int v[510][510]; // 记录每…

【Linux】第十一章 管理网络

目录 1.TCP/IP网络模型 物理层&#xff08;Physical&#xff09; 数据链路层&#xff08;Date Link&#xff09; 网络层&#xff08;Internet&#xff09; 传输层&#xff08;Transport&#xff09; 应用层&#xff08;Application&#xff09; 2. 对于 IPv4 地址&#…

python_股票月数据趋势判断

目录 前置 代码 视频&月数据 前置 1 A股月数据趋势大致判断&#xff0c;做一个粗略的筛选 2 逻辑&#xff1a; 1&#xff09;取最近一次历史最高点 2&#xff09;以1&#xff09;中最高点为分界点&#xff0c;只看右侧数据&#xff0c;取最近一次最低点 3&#xf…

Python PyAutoGUI库【GUI 自动化库】深度解析与实战指南

一、核心工作原理 底层驱动机制&#xff1a; 通过操作系统原生API模拟输入使用ctypes库调用Windows API/Mac Cocoa/Xlib屏幕操作依赖Pillow库进行图像处理 事件模拟流程&#xff1a; #mermaid-svg-1CGDRNzFNEffhvSa {font-family:"trebuchet ms",verdana,arial,sans…

Spring框架allow-bean-definition-overriding详细解释

Spring框架中&#xff0c;allow-bean-definition-overriding 是一个控制是否允许覆盖同名Bean定义的配置属性。以下是详细说明&#xff1a; ​1. 作用​ ​允许/禁止Bean定义覆盖​&#xff1a;当Spring容器中检测到多个同名的Bean定义时&#xff0c;此配置决定是否允许后续的…

机器人抓取位姿检测——GRCN训练及测试教程(Pytorch)

机器人抓取位姿检测——GRCN训练及测试教程(Pytorch) 这篇文章主要介绍了2020年IROS提出的一种名为GRCN的检测模型,给出了代码各部分的说明,并给出windows系统下可以直接复现的完整代码,包含Cornell数据集。 模型结构图 github源码地址:https://github.com/skumra/robo…

在web应用后端接入内容审核——以腾讯云音频审核为例(Go语言示例)

腾讯云对象存储数据万象&#xff08;Cloud Infinite&#xff0c;CI&#xff09;为用户提供图片、视频、语音、文本等文件的内容安全智能审核服务&#xff0c;帮助用户有效识别涉黄、违法违规和广告审核&#xff0c;规避运营风险。本文以音频审核为例给出go语言示例代码与相应结…

GraphRAG知识库概要设计展望

最近研究了一下GraphRAG&#xff0c;写了一个文档转换工具还有图可视化工具&#xff0c;结合langchain构建RAG经验&#xff0c;还有以前的数据平台&#xff0c;做了一个知识库概要设计&#xff0c;具体应用欢迎留言探讨。 一、GraphRAG整体概述 GraphRAG图基检索增强生成&…

Android Studio 日志系统详解

文章目录 一、Android 日志系统基础1. Log 类2. 日志级别 二、Android Studio 中的 Logcat1. 打开 Logcat2. Logcat 界面组成3. 常用 Logcat 命令 三、高级日志技巧1. 自定义日志工具类2. 打印方法调用栈3. 打印长日志4. JSON 和 XML 格式化输出 四、Logcat 高级功能1. 自定义日…

深度对比:Objective-C与Swift的RunTime机制与底层原理

1. RunTime简介 RunTime&#xff08;运行时&#xff09;是指程序在运行过程中动态管理类型、对象、方法等的机制。Objective-C 和 Swift 都拥有自己的运行时系统&#xff0c;但设计理念和实现方式有很大不同。理解 RunTime 的底层原理&#xff0c;是掌握 iOS 高级开发的关键。…

使用手机录制rosbag包

文章目录 简介录制工具录制步骤录制设置设置IMU录制频率设置相机分辨率拍照模式录制模式数据制作获取数据数据转为rosbag查看rosbag简介 ROS数据包(rosbag)是ROS系统中用于记录和回放传感器数据的重要工具,通常用于算法调试、系统测试和数据采集。传统上,rosbag依赖于ROS环…

浅谈PCB传输线(一)

前言&#xff1a;浅谈传输线的类型&#xff0c;以及传输线的一些行为特性。 1.传输线的种类 2.互连线被视为传输线的场景 3.传输线的行为特性*** 1.传输线的种类 PCB 中的信号传输线通常有两种基本类型: 微带线和带状线。此外&#xff0c;还有第三种类型–共面线(没有参考平面…

【angular19】入门基础教程(一):项目的搭建与启动

angular现在发展的越来越能完善了&#xff0c;在vue和react的强势竞争下&#xff0c;它迎来了自己的巨大变革。项目工程化越来越好&#xff0c;也开始拥抱了vite这种高效的构建方式。所以&#xff0c;我们有必要来学习这么一个框架了。 项目实现效果 nodejs环境 Node.js - v^…

在前端应用领域驱动设计(DDD):必要性、挑战与实践指南

引言 领域驱动设计&#xff08;Domain-Driven Design&#xff0c;简称 DDD&#xff09;起源于后端复杂业务系统建模领域&#xff0c;是 Eric Evans 在 2003 年提出的一套理论体系。近年来&#xff0c;随着前端工程化与业务复杂度的持续提升&#xff0c;"前端也要 DDD&quo…