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.pswp.cn/web/77580.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/77580.shtml
英文地址,請注明出處:http://en.pswp.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…