SELECT `t`.`ID`, `t1`.`VALUE`, `t2`.`VALUE`, ..., `tN`.`VALUE`, `t`.`TIME`
FROM (
SELECT DISTINCT `ID`, `TIME`
FROM `testtable`
) AS `t`
LEFT JOIN `testtable` AS `t1` ON `t1`.`NAME` = :name1
AND `t1`.`ID` = `t`.`ID` AND `t1`.`TIME` = `t`.`time`
LEFT JOIN `testtable` AS `t2` ON `t2`.`NAME` = :name2
AND `t2`.`ID` = `t`.`ID` AND `t2`.`TIME` = `t`.`time`
...
LEFT JOIN `testtable` AS `tN` ON `tN`.`NAME` = :nameN
AND `tN`.`ID` = `t`.`ID` AND `tN`.`TIME` = `t`.`time`
СREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a CHAR(16);
DECLARE b INT;
DECLARE cur CURSOR FOR SELECT id, data FROM t1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
read_loop: LOOP
FETCH cur INTO a, b;
IF done THEN
LEAVE read_loop;
END IF;
здесь выполняются нужные действия
END LOOP;
CLOSE cur;
END
SELECT `time`, GROUP_CONCAT(`worker`)
FROM `table`
GROUP BY `time`
SELECT `os`.`service_cost`
FROM `Org_services` AS `os`
JOIN `Organizations` AS `o` ON `o`.`ID` = `os`.`org_id` AND `o`.`org_name` = :orgName
JOIN `Services_list` AS `s` ON `s`.`ID` = `os`.`service_id` AND `s`.`service_name` = :servName
SELECT DATE(`date_insert`) AS `date`, SUM(`price`), AVG(`price`), COUNT(*)
FROM `b_sale_order`
WHERE (`canceled` = 'N') AND (`date_insert` BETWEEN :StartD AND :EndD)
GROUP BY `date`
ORDER BY `date`