简体   繁体   English

7个SQL行中的第一个开始时间和最后一个结束时间,仅显示1个SQL结果

[英]First start_time and last end_time from 7 SQL rows to show only 1 SQL result

I'm making a timesheet submit/approve function and currently working on the pending.php page, which the manager/admin can go to pending.php and view the pending timesheets for review... 我正在执行时间表提交/批准功能,当前正在开发未决.php页面,经理/管理员可以转到未决.php并查看未决时间表以进行审查...

my code now is: 我的代码现在是:

list($qh,$num) = dbQuery(
"SELECT start_time, end_time, uid, submitid, submitstatus, submitdate, submitapprover 
FROM $TIMES_TABLE 
WHERE submitstatus=1 
ORDER BY submitid");

right now it shows all the timesheet entries for that week: example 现在,它显示了该周的所有时间表条目: 示例

what I really need is just one line for each week submitted. 我真正需要的是每个星期提交一行。 Basically, grabbing the first start_time and the last end_time and making it together (start - end) (start_time - end_time | username | id | submitdate | submit status..etc) 基本上,获取第一个开始时间和最后一个结束时间并将其组合在一起(开始-结束)(开始时间-结束时间|用户名| ID |提交日期|提交状态..etc)

Someone told me to use group_catcon or something but I'm unfamiliar with that. 有人告诉我使用group_catcon之类的东西,但我对此并不熟悉。

From my pic I would want something like: 从我的照片中,我想要类似的东西:

2012-12-30 - 2013-01-05 | admin | submitid#### | submitdate | status | approver
2013-01-06 - 2013-01-09 | admin | submitid#### | submitdate | status | approver

I'm pretty new to php/mysql so my apologies 我是php / mysql的新手,所以我道歉

You may find with all these columns it divides things up more than you want. 您可能会发现,通过所有这些列,它可以将内容分割得更多。 For example of there's various approvers. 例如,有各种批准者。 To that end you may want to remove some from the query. 为此,您可能需要从查询中删除一些内容。

select 
  concat(min(start_time), ' - ', max(end_time)), 
  uid, 
  submitid, 
  submitstatus, 
  submitdate, 
  submitapprover
FROM 
  $TIMES_TABLE
WHERE 
  submitstatus=1 
GROUP BY
  uid, 
  submitid, 
  submitstatus, 
  submitdate, 
  submitapprover
ORDER BY 
  submitid

I have built many timesheet applications in many languages. 我已经用多种语言构建了许多时间表应用程序。 You can use group_concat it ties strings together with a comma, but i do not think that is what you need. 您可以使用group_concat将字符串与逗号联系在一起,但是我认为这不是您所需要的。

The catch is that you have to use it together with group by. 要注意的是,您必须将其与group by一起使用。 From your db structure it might not work for you. 从您的数据库结构来看,它可能对您不起作用。

The rule with "group by" is that you have to select columns that are either used in the "group by" list of columns or you have to use an aggregate function on that column. 使用“分组依据”的规则是,您必须选择在“分组依据”列列表中使用的列,或者必须在该列上使用聚合函数。

on a books table that has the following structure books_sold (id, author_id, date, price) you can do the following operations 在具有以下结构books_sold(id,author_id,date,price)的books表上,可以执行以下操作

select sum(price), author FROM books_sold group by author

To get the total sum of the books per author. 获取每个作者的书籍总数。

select sum(price), date FROM books_sold group by date

to get the total sum of the books sold per date. 以获取每个日期售出的图书的总金额。

select group_concat(id), date FROM books_sold group by date

TO get the the id of all the books sold per date. 获取每个日期售出的所有书籍的ID。 The ids will be separated by comma ID将以逗号分隔

But you cannot do a 但是你不能做

select group_concat(id), date, author FROM books_sold group by date

because on the author column you are not doing a group by, or a mass operation. 因为在作者列上,您不是在进行分组或大规模操作。 The query works but the author column is not reliable. 该查询有效,但是author列不可靠。

Now from your db structure, I do not think you can do a group_concat and still get the fields you desire. 现在,从您的数据库结构中,我认为您无法执行group_concat并仍然获得所需的字段。 What happens if the approver is not the same guy in a week? 如果批准者一周内不在同一个人,会发生什么? Your approver column will not make sense. 您的批准者专栏将毫无意义。 What happens if the submitid is not the same in an entire week? 如果整个一周中的submitid不一样怎么办? What happens if the submitdate is not the same on an entire week? 如果整个星期的提交日期都不相同怎么办?

If those columns are always the same you can do a 如果这些列始终相同,则可以执行

select CONCAT(min(start_time), ' - ', max(end_time)), uid, submitid, submitstatus, submitdate, submitapprover FROM $TIMES_TABLE WHERE submitstatus=1 GROUP BY uid, submitid, submitstatus, submitdate, submitapprover ORDER BY submitid

暂无
暂无

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

相关问题 sql使用start_time和end_time进行复杂数据排序 - sql Complex data sorting with start_time and end_time Laravel 5.1获取start_time和end_time之间的行(两个不同的字段) - Laravel 5.1 Fetch Rows between start_time and end_time (two different fields) 使用 PHP 从具有 start_time 和 end_time 的多维数组中查找总小时数? - Find total hours from multi-dimensional array with start_time and end_time using PHP? 获取每个日期 PHP 的 start_time 和 end_time 的总小时数? - Get total hours from start_time and end_time for each date PHP? 我想要`start_time` 和`end_time` 计数如何得到这个? - I want to `start_time` and `end_time` count how can get this? 如何在yii2中使用start_time和end_time获取MySql中已有的记录? - How to fetch already existing records in MySql with start_time and end_time in yii2? PayPal + PHP - 获取收款(开始时间/结束时间) - PayPal + PHP - Fetch incoming payments (start_time / end_time) 如果在范围内找到,如何找到 start_time 和 end_time 范围,然后检查 start_date 和 end_date - How to find the start_time and end_time range if found in the range then check start_date and end_date 我想显示当前时间不在(column)start_time和end_time(column)之间的所有人员记录 - I want to display all personnel records that the current time is not between to the (column)start_time and end_time(column) 如何使用php从sql显示开始日期和结束日期之间或开始时间和结束时间之间的数据? - How to display data between start date and end date or start time and end time from sql using php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM