處理SQL分組與排序中多個結果行
在對資料進行分組和排序的查詢中,經常會遇到這種情況:具有相同分組鍵的多個行,但其他列的值不同。這可能會妨礙獲得所需的匯總數據。
例如,一個按硬體型號分組資料的查詢可能會傳回多行,這些行對於同一型號具有不同的「結果」。為了將此壓縮成每個型號一行,可以修改查詢,將實際的0值的結果值修改為0,非零值修改為1。但是,這種方法對於具有多個“結果”值的型號仍然會產生多行。
實現所需聚合的關鍵是按CASE表達式分組,而不是按表示結果的來源列分組。透過按條件值分組,查詢可以合併具有相同分組鍵和條件結果的行。
例如,以下查詢按型號名稱、嘗試類型和用於轉換結果值的CASE表達式進行分組:
SELECT CURRENT_DATE-1 AS day, model.name, attempt.type, CASE WHEN attempt.result = 0 THEN 0 ELSE 1 END, count(*) FROM attempt attempt, prod_hw_id prod_hw_id, model model WHERE time >= '2013-11-06 00:00:00' AND time < '2013-11-07 00:00:00' GROUP BY model.name, attempt.type, CASE WHEN attempt.result = 0 THEN 0 ELSE 1 END ORDER BY model.name, attempt.type, CASE WHEN attempt.result = 0 THEN 0 ELSE 1 END;
或者,可以使用CASE表達式的列別名來區分它與結果來源列:
SELECT CURRENT_DATE-1 AS day, model.name, attempt.type, CASE WHEN attempt.result = 0 THEN 0 ELSE 1 END AS result1, count(*) FROM attempt attempt, prod_hw_id prod_hw_id, model model WHERE time >= '2013-11-06 00:00:00' AND time < '2013-11-07 00:00:00' GROUP BY model.name, attempt.type, result1 ORDER BY model.name, attempt.type, result1;
重要的是要記住,使用位置引用(例如,「GROUP BY 1,2,3」)按CASE表達式分組比在GROUP BY子句中使用列名更能抵抗SELECT列表中的更改。
以上是在 SQL 中對資料進行分組和排序時如何處理多個結果行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

mysqlblobshavelimits:tinyblob(255bytes),blob(65,535 bytes),中間佈洛布(16,777,215個比例),andlongblob(4,294,967,967,295 bytes).tousebl觀察:1)考慮pperformance impactsandSandStorLageBlobSextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

自動化在MySQL中創建用戶的最佳工具和技術包括:1.MySQLWorkbench,適用於小型到中型環境,易於使用但資源消耗大;2.Ansible,適用於多服務器環境,簡單但學習曲線陡峭;3.自定義Python腳本,靈活但需確保腳本安全性;4.Puppet和Chef,適用於大規模環境,複雜但可擴展。選擇時需考慮規模、學習曲線和集成需求。

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)轉換theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchUsiseLike.2)forCompresseBlysBlobs,useuncompresseblobs,useuncompressbeforeconversion.3)expperformance impperformance imptactSandDataEcoding.4)

mysqloffersvariousStringDatatYpes:1)charforfixed Lengtth Strings,IdealforConsistLengthDatalikeCountryCodes; 2)varcharforvariable長度長,合適的forfieldslikenames; 3)texttypefesforepesforlargertext,forforlargertext,goodforforblogblogpostsbutcan impactcuctcuctcuctpercrance; 4)biland;

tomasterMysqlblobs,關注台詞:1)ChooseTheApprProbType(tinyBlob,blob,blob,Mediumblob,longblob)基於dongatasize.2)InsertDatausingload_fileforefice.3)

blobdatatypesinmysqlareusedforvorvoringlargebinarydatalikeimagesoraudio.1)useblobtypes(tinyblobtolonglongblob)基於dondatasizeneeds。 2)庫孔素pet petooptimize績效。 3)考慮Xternal Storage Forel Blob romana databasesizerIndimprovebackupe

toadDuserStomySqlfromtheCommandline,loginasroot,thenusecreateuser'username'@'host'host'Indessifiedby'password'; tocreateanewuser.grantpermissionswithgrantprantallprivilegesondatabase

mySqlofferSeightStringDatateTypes:char,varchar,二進制,二進制,varbinary,blob,文本,枚舉,枚舉和set.1)長度,理想的forconsistentDatatalIkeCountryCodes.2)varcharisvariable長度,長度,效率foriforitifforiticforiticforiticforiticforiticforitic forvaryingdatalikename.3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具