简体   繁体   English

从 MySql 中的日期时间格式按日期名称对同一天进行分组

[英]Group same Days by name of day from Datetime Format in MySql

I have column named start im my database where i store thousands of dates (not the ones you can eat) in the following format:我的数据库中有一个名为start的列,我在其中存储了数千个dates (不是你可以吃的日期),格式如下:

2019-05-04 07:30:00

To get some stats im using google.charts and trying to group the values by the name of days (Monday, Thuesday).要使用google.charts获取一些统计信息,并尝试按日期名称(星期一、星期二)对值进行分组。

So far I have the following.到目前为止,我有以下内容。 Any idea how to extract the day name from the datetime and group them by the same names to get something like:知道如何从日期时间中提取日期名称并将它们按相同的名称分组以获得如下内容:

Monday 120星期一 120

Thuesday 236星期二 236

Wednesday 987星期三 987

and so on等等

在此处输入图像描述

Database Structure:数据库结构:

-- phpMyAdmin SQL Dump
-- version 4.9.7deb1
-- https://www.phpmyadmin.net/
CREATE TABLE `test` (
  `id` int(10) UNSIGNED NOT NULL,
  `start` datetime NOT NULL,
  `end` datetime DEFAULT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4  COLLATE=utf8mb4_unicode_ci;

Select Statement so far: Select 目前声明:

$sql_8 = "SELECT `start` count(*) as number FROM test GROUP BY `start` ";

PHP Query and Script part: PHP 查询和脚本部分:

<?php   
    $result_8 = mysqli_query($conn, $sql_8); 
?> 

<script type="text/javascript">
    google.charts.load('current', {'packages':['table']});
    google.charts.setOnLoadCallback(drawTable);
function drawTable() {
    
    
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Day');
        data.addColumn('number', 'Count');
        data.addRows([ 
        
            <?php  
                while($row = mysqli_fetch_array($result_8)) { 
                    
                    $date = $row["start"];
                    echo "[ ' ".$date." ', ".$row["number"]." ],";   
                }
            ?>          
                  
        ]); 
                     
        var table = new google.visualization.Table(document.getElementById('table_div_8'));
        table.draw(data, {showRowNumber: true, width: '100%', height: '100%',});
    }  
</script> 

The MySql / MariaDB function DAYOFWEEK(start) gives you a number 1-7 (1 == Sunday) for any DATE, DATETIME, or TIMESTAMP value. MySql / MariaDB function DAYOFWEEK(start)为任何 DATE、DATETIME 或 TIMESTAMP 值提供数字 1-7(1 == 星期日)。 So if you GROUP BY DAYOFWEEK(start) ORDER BY DAYOFWEEK(start) you will get a useful result set in a useful order.因此,如果您GROUP BY DAYOFWEEK(start) ORDER BY DAYOFWEEK(start)您将以有用的顺序获得有用的结果集。

Something like this query will work.像这个查询这样的东西会起作用。

SELECT DAYNAME(start) weekday,
       count(*) as number
  FROM test 
 GROUP BY DAYOFWEEK(start)
 ORDER BY DAYOFWEEK(start);

Pro tip : Every minute you spend studying your RDBMS's date and time arithmetic operations will save you hours in the future.专业提示:您花在研究 RDBMS 的日期和时间算术运算上的每一分钟都会在未来节省您的时间。

If you're looking to group by day name regardless of in what year or month the date is, then you can use MySQL DAYNAME() function:如果您希望按日期名称分组,而不管日期是在哪一年或哪一个月,那么您可以使用 MySQL DAYNAME() function:

SELECT DAYNAME(`Start`) AS Daynm,
       COUNT(*) AS number
  FROM test
GROUP BY Daynm;

And maybe if you want to order by day ascending, then add another function WEEKDAY() - Monday as it's first day;也许如果您想按天升序排序,则添加另一个 function WEEKDAY WEEKDAY() - Monday ,因为它是第一天; or DAYOFWEEK() - Sunday as it's first day.DAYOFWEEK() - 周日,因为它是第一天。

SELECT WEEKDAY(`Start`) AS Wkday,
       DAYNAME(`Start`) AS Daynm,
       COUNT(*) AS number
  FROM test
GROUP BY Wkday, Daynm
  ORDER BY Wkday;

Demo fiddle演示小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM