是否可以在一个mysql查询中执行此操作

时间:2013-01-26 19:28:26

标签: mysql

这可以通过一个查询完成吗?

表日:

dID
enable
holiday
fewMore

表格段:

d_ID //foregn key to day->dID
startTime
endTime
fewMore

查询:

SELECT dID, enabled,holiday,  
       TIME_TO_SEC(dHaurs) as wsTimeHHmm,
       DATE_FORMAT(dDate, '%d') as dateForComparing,
       DATE_FORMAT(start, '%H:%i') as startTime,
       DATE_FORMAT(end, '%H:%i') as endTime 
FROM day d                                  
LEFT JOIN segment ws 
ON ws.d_ID = d.dID
WHERE dDate BETWEEN '" . $startDate . "' AND  '" . $endDate . "'                                
ORDER by dateForComparing

结果我得到了:

dID:10, start time:08:00, end time:09:00, holiday:0, enabled:1
dID:10, start time:09:00, end time:10:00, holiday:0, enabled:1
dID:11, start time:08:00, end time:10:00, holiday:1, enabled:1

但我想用相同的d_ID对所有段进行分组。

dID:10, array((start time:08:00, end time:09:00),(start time:09:00, end time:10:00)), holiday:0, enabled:1

我尝试使用GROUP_CONCAT(wsID,start, end) ... GROUP BY dID,但我不确定是否正确,而且我无法使用函数DATE_FORMAT(start, '%H:%i') as startTime

这可以只使用mysql吗?

更新: 现在,我正在从结果创建数组:

  $returnData  [] =array(array(dID =>"dID",
                         endTime => $end_time,
                         startTime=> $start_time,...
                               ),
                        array( enabled=> $enabled,
                                holiday =>$holiday
                               )
                         );
echo json_encode($returnData);

1 个答案:

答案 0 :(得分:0)

你不能在MySQL中返回数组,但你可以像这样操作输出:

dID:10, start time:08:00, end time:10:00, holiday:0, enabled:1