按非数据库字段分组

发布于 2024-10-28 00:08:38 字数 933 浏览 1 评论 0原文

如何按未保存在数据库中的字段对查询结果进行分组。

例如,我想按持续时间对结果进行分组,该持续时间是从开始时间和结束时间相减得出的。

这是我如何找出持续时间的方法

date1= $row_TicketRS['CloseDate']; 
$date2 = $row_TicketRS['OpenDate'];

$diff = abs(strtotime($date2) - strtotime($date1)); 

$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));


if ( $days > 0)

{

$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];

$t1=($time1); 
$t2=($time2);

$end=('14:30');
$start=('07:30');

$n = $end- $t1;

$n2 = $t2- $start;

$Hours2 = floor(($n+$n2)+(($days-1)*7));

echo   $Hours2.' Hours'; 

,但我知道我不知道如何将其添加到查询中,

这是我的查询

$strQuery = "SELECT count(`ticket`.TicketID) as TotOutput, department.`DeptName` FROM `ticket`, `user`, department where ticket.OwnerID = user.EmpNo and  user.`DepartmentID` = department.`DepartmentID` and OpenDate between'".$DateFrom."' And '".$DateTo."'"

How can I group a query result by a field that is not saved in the database.

For example I want to group the result by duration which is came from subtraction of start time and end time.

here is how i find out the duration

date1= $row_TicketRS['CloseDate']; 
$date2 = $row_TicketRS['OpenDate'];

$diff = abs(strtotime($date2) - strtotime($date1)); 

$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));


if ( $days > 0)

{

$time1 = $row_TicketRS['OpenTime'];
$time2= $row_TicketRS['CloseTime'];

$t1=($time1); 
$t2=($time2);

$end=('14:30');
$start=('07:30');

$n = $end- $t1;

$n2 = $t2- $start;

$Hours2 = floor(($n+$n2)+(($days-1)*7));

echo   $Hours2.' Hours'; 

but know i do not know how to add it to the query

here is my query

$strQuery = "SELECT count(`ticket`.TicketID) as TotOutput, department.`DeptName` FROM `ticket`, `user`, department where ticket.OwnerID = user.EmpNo and  user.`DepartmentID` = department.`DepartmentID` and OpenDate between'".$DateFrom."' And '".$DateTo."'"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归途 2024-11-04 00:08:38

最好有详细信息,但派生表/内联视图将允许您按计算值进行分组:

  SELECT x.duration, 
         COUNT(*)
    FROM (SELECT t.col, 
                 t.end_time - t.start_time AS duration
            FROM YOUR_TABLE t) x
GROUP BY x.duration

It'd be better to have details, but a derived table/inline view would allow you to group by a computed value:

  SELECT x.duration, 
         COUNT(*)
    FROM (SELECT t.col, 
                 t.end_time - t.start_time AS duration
            FROM YOUR_TABLE t) x
GROUP BY x.duration
相守太难 2024-11-04 00:08:38

如何使用如下别名将该计算值添加到查询中:

SELECT some_fields, end - start AS duration FROM table ORDER BY duration

How about adding that computed value to the query with an alias like this:

SELECT some_fields, end - start AS duration FROM table ORDER BY duration
九公里浅绿 2024-11-04 00:08:38

不要为隐藏列添加别名,直接使用

exmaple:

SELECT id, FLOOR(value/100)
  FROM tbl_name
  GROUP BY id, FLOOR(value/100);

参考

MySQL 允许 GROUP BY 中的表达式
子句,因此不需要别名:

dont put alias for hidden column , use directly

exmaple:

SELECT id, FLOOR(value/100)
  FROM tbl_name
  GROUP BY id, FLOOR(value/100);

Reference

MySQL permits expressions in GROUP BY
clauses, so the alias is unnecessary:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文