简体   繁体   English

如何检查表中是否已有名称? 查询Mysql每天的总和,然后通过PHP填充HTML数据

[英]How to check if name already in the table? Querying Mysql for sum of each day and then populating an HTML data through PHP with it

So, this is my first question here, and I'd really appreciate if someone could help me. 所以,这是我的第一个问题,如果有人能帮助我,我真的很感激。 I currently have a Mysql database and a web app made with PHP where the users can input how much time they have used on a specific task that day. 我目前有一个Mysql数据库和一个用PHP制作的网络应用程序,用户可以输入他们当天在特定任务上使用了多少时间。

Right now I would like to populate an HTML table with the SUM of each days activity in hours. 现在我想用几小时填充每天活动的SUM的HTML表格。 My problem is that I can't find a way to check if the name field is unique, as the data contains many rows with the same name field, one for each day. 我的问题是我找不到检查名称字段是否唯一的方法,因为数据包含许多具有相同名称字段的行,每天一个。

I have this Mysql query: 我有这个Mysql查询:

SELECT 
    t_email.name, 
    DATE_FORMAT(input_date,'%d') AS days, 
    SUM(time_worked) AS totalTime
FROM 
    t_transactions 
    LEFT JOIN t_email ON t_transactions.email_id >= t_email.email_id 
WHERE 
    DATE_FORMAT(input_date,'%m') = '02'
    AND DATE_FORMAT(input_date,'%Y') = '2019' 
GROUP BY DATE_FORMAT(input_date,'%d') 
ORDER BY t_email.name

Which returns me a table like this: 这给我一个像这样的表:

name  days  totalTime
james 01    5.00 
james 06    2.00 
jimmy 02    4.0

And so on. 等等。 I'm using the below PHP: 我正在使用以下PHP:

<?php 
foreach ($monthly_data_per_day as $row): ?>
<tr>
    <td><?= $row['name'] ?></td>
    <?php
    $num_days = date('t');
    for ($i = 1; $i <= $num_days; $i++) {
        if ($row['days'] == $i) {
            echo "<td>" . $row['totalTime'] . "</td>";
        } else {
            echo "<td>" . 0 . "</td>";
        }
    }?></tr>
<?php endforeach;?>

I'd like to have an HTML table where after each name in the database, you could see how many hours that person has done that day. 我想要一个HTML表格,在数据库中的每个名字之后,你可以看到那个人当天完成了多少小时。 So columns from 1 to how many days there are in the month. 所以列中从1到本月有多少天。

Right now I get a row for each day, with the correct sum of time worked on just one column, as there's one row per column. 现在我每天都会得到一行,正确的时间总和就在一列上,因为每列有一行。

Here's a picture of what I'd like the table to look like: 这是我想要的表格的图片:

A picture of the table with columns for each day of the month 桌子的图片与每月的每一天的列

There is a solution for this using conditional aggregation... that will requires you to write 31 CASE statements, like : 有一个解决方案使用条件聚合...这将要求您编写31个CASE语句,如:

SELECT 
    e.name, 
    SUM(CASE WHEN DAYOFMONTH(t.input_date) = 1 THEN t.time_worked ELSE 0 END) AS day1,
    SUM(CASE WHEN DAYOFMONTH(t.input_date) = 2 THEN t.time_worked ELSE 0 END) AS day2,
    SUM(CASE WHEN DAYOFMONTH(t.input_date) = 3 THEN t.time_worked ELSE 0 END) AS day3,
    ...
FROM 
    t_transactions t
    LEFT JOIN t_email e ON t.email_id = e.email_id 
WHERE YEAR(input_date) = 2019 AND MONTH(t.input_date) = 2
GROUP BY e.name
ORDER BY e.name

Other remarks : 其他评论:

  • I guess that you meant t.email_id = e.email_id instead of t.email_id >= e.email_id 我猜你的意思是t.email_id = e.email_id而不是t.email_id >= e.email_id

  • it will probably more efficient to use MySQL built-in date functions such as YEAR , DAYOFMONTH and MONTH , which return integers, rather than manipulating formated string representations of dates 使用MySQL内置日期函数(例如YEARDAYOFMONTHMONTH )返回整数可能更有效,而不是操纵日期的格式化字符串表示

  • as not all of the fields in your query were aliase, I made the assumption that columns input_date and time_worked belong to table t_transactions . 由于查询中的所有字段都不是别名,我假设列input_datetime_worked属于表t_transactions

  • since we SELECT e.name , this column needs to appear in the GROUP BY clause ; 因为我们SELECT e.name ,所以此列需要出现在GROUP BY子句中; on non-ancient versions of MySQL, this is a syntax error 在非古代版本的MySQL上,这是一个语法错误

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

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