欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql中的多次分组。(表中重复的信息,处理方式)

程序员文章站 2022-09-05 13:29:26
<![CDATA[
	  SELECT
  a2.plateNo,v.plateColor,d.name as depName,COUNT(a2.plateNo)*60*24 AS onlineTime,(TIMESTAMPDIFF(DAY,DATE_FORMAT(MIN(a2.createDate), '%Y-%m-%d'),DATE_FORMAT(MAX(a2.createDate), '%Y-%m-%d'))+1)*60*24 as totalTime
  ,COUNT(a2.plateNo)/(TIMESTAMPDIFF(DAY,DATE_FORMAT(MIN(a2.createDate), '%Y-%m-%d'),DATE_FORMAT(MAX(a2.createDate), '%Y-%m-%d'))+1)*100 as onlineRate
FROM
  (SELECT
    o.plateNo,o.createDate
    FROM
    onlinerecord o
  WHERE alarmType = "GpsOnline"
  GROUP BY plateNo,
    DATE_FORMAT(createDate, '%Y-%m-%d')) a2,department d,vehicle v
]]>
		<where>
			LEFT JOIN (SELECT * FROM vehicle GROUP BY plateNo) v ON a2.plateNo = v.plateNo

			LEFT JOIN department d ON v.depId = d.depId
mysql语句解释
(1)from中有一个表示a2
SELECT
 o.plateNo,o.createDate
  FROM
  onlinerecord o
  WHERE alarmType = "GpsOnline"
  GROUP BY plateNo,
  DATE_FORMAT(createDate, '%Y-%m-%d')

进行2次分组。 第一次分组按车牌号进行分组;
第二次分组按日期(精确到日)进行分组;
下面的left join(目的是:以a2为主体);
其中对vehicle进行按plateNo(其中的vehicle表中大量重复的信息。这样分组后,即可解决)

<![CDATA[]]>自己百度。这里我觉得可以去掉。



本文地址:https://blog.csdn.net/deadchance/article/details/108993318

相关标签: java mysql