处理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)考虑performance impactsandSandStorelargeblobsextern; 2)管理backbackupsandreplication carecration; 3)usepathsinst

自动化在MySQL中创建用户的最佳工具和技术包括:1.MySQLWorkbench,适用于小型到中型环境,易于使用但资源消耗大;2.Ansible,适用于多服务器环境,简单但学习曲线陡峭;3.自定义Python脚本,灵活但需确保脚本安全性;4.Puppet和Chef,适用于大规模环境,复杂但可扩展。选择时需考虑规模、学习曲线和集成需求。

是的,YouCansearchInIdeAblobInMysqlusingsPecificteChniques.1)转换theblobtoautf-8StringWithConvertFunctionWithConvertFunctionandSearchusiseLike.2)forCompresseBlyblobs,useuncompresseblobs,useuncompressbeforeconversion.3)acpperformance impperformance imperformance imptactsanddataEccoding.4)

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

TomasterMySQLBLOBs,followthesesteps:1)ChoosetheappropriateBLOBtype(TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOB)basedondatasize.2)InsertdatausingLOAD_FILEforefficiency.3)Storefilereferencesinsteadoffilestoimproveperformance.4)UseDUMPFILEtoretrieveandsaveBLOBsco

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
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

记事本++7.3.1
好用且免费的代码编辑器