如何在group_concat中订购所有值?

时间:2018-01-25 09:46:14

标签: mysql sql sql-order-by case group-concat

查询: -

$str = 'ID:40021223';   //pass any string
echo $output = getID($str); 

function getID($str) {
   $res = explode(':',$str);  //split string with :
   if(in_array('ID',$res)) {  //if array contain Id then it will give the exact output else null
      return $res[1];
   } else {
      return null;
   }
}

实际oupput: -

Employee Id  Employee Name Log Timings

00000247  M. Rama Rao  18:03,17:40,13:35,11:48,09:19

00000247  M. Rama Rao  15:24,09:11,18:44,16:37,15:27,15:24

但需要:

Employee Id  Employee Name Log Timings

00000247  M. Rama Rao  09:19,11:48,13:35,17:40,18:03

00000247  M. Rama Rao  09:11,15:27,15:24,15:24,16:37,18:44

2 个答案:

答案 0 :(得分:1)

您可以在const $ = require('jquery') $.post("https://ticketsoko.nouveta.co.ke/api/index.php", { TransactionType: "getEvents" }, function (data, status) { var info = JSON.parse(data); for (var i = 0; i < info.data.length; i++) { ticketSales[i] = info.data[i].totalTicketsSalesAmount; events[i] = info.data[i].Events.events_name; console.log(events[i]); } }) 内添加ORDER BY

我不理解GROUP_CONCAT的逻辑,它始终是WHEN log_date = log_date。然后,没有必要使用true语句,而不会属于CASE个案。

ELSE

 group_concat(CASE WHEN log_date = log_date THEN log_time ELSE '' END)

两者都会给你相同的结果。因此,您应该进行以下更改

group_concat(log_time)

答案 1 :(得分:0)

注意:

  • 从不FROM子句中使用逗号。始终使用正确的,明确的JOIN语法。
  • 您按log_date进行汇总,因此case中不需要group_concat() EXPRESSION
  • 最佳做法是将所有非汇总列放入GROUP BY
  • 我建议在SELECT中添加日期和班次。
  • at_hr_emp的表别名为employee(在您的代码中),但引用为emp。只需使用缩写e

因此,此查询应写为:

SELECT e.emp_id AS employeeId, e.emp_name AS employeeName, l.log_date,
       group_concat(log_time order by log_time) as logTimings,
       shift AS shift
FROM at_hr_logs_Jan l JOIN
     at_hr_emp e
     ON l.user_id = e.emp_code JOIN
     at_dates dt
     ON l.log_date = dt.att_date
WHERE e.user_id=00000247 AND e.dept_id = 1 AND
      l.status_id = fn_getcodevalue_id('STS', 'ACTIVE')
GROUP BY e.emp_id, e.emp_name, l.log_date, shift;