mysql直接从复杂的select中插入

时间:2013-06-23 19:09:06

标签: mysql select insert

我有这个MySQL SELECT:

SELECT t.*,
 sum(HOUR(TIMEDIFF(STR_TO_DATE(Arbeitsbeginn, '%d.%m.%Y %H:%i:%s'),
 STR_TO_DATE(Arbeitsende, '%d.%m.%Y %H:%i:%s')))) AS ARBEITSZEIT,
sum(HOUR(TIMEDIFF(STR_TO_DATE(Projektbeginn, '%d.%m.%Y %H:%i:%s'),
STR_TO_DATE(Projektende, '%d.%m.%Y %H:%i:%s')))) AS Projektzeit

FROM (SELECT DriverName,
         Baustelle,
         Arbeitsbeginn,
         Arbeitsende,
        Projektbeginn,
        Projektende
  FROM (SELECT DRIVERNAME,
               GROUP_CONCAT(IF(ACTIONTEXT = 'PB', AREA, NULL)) AS 'Baustelle',
               GROUP_CONCAT(IF(ACTIONTEXT = 'Arbeitsbeginn', DATETIME, NULL)) AS 'Arbeitsbeginn',
               GROUP_CONCAT(IF(ACTIONTEXT = 'Arbeitsende', DATETIME, NULL)) AS 'Arbeitsende',
               GROUP_CONCAT(IF(ACTIONTEXT = 'PB', DATETIME, NULL)) AS 'Projektbeginn',
               GROUP_CONCAT(IF(ACTIONTEXT = 'PE', DATETIME, NULL)) AS 'Projektende'
        FROM geoImportRoot
        GROUP BY DRIVERNAME
       ) A CROSS JOIN
       (SELECT 1 AS n UNION ALL SELECT 2) n
) t
GROUP BY DriverName;

是否可以直接将其插入新表中?

1 个答案:

答案 0 :(得分:1)

要从select语句创建 new 表,您可以使用:

CREATE TABLE .... SELECT

它基本上是CREATE TABLE语句,附加了SELECT语句。


如果您希望将SELECT结果插入现有表中,请使用INSERT ... SELECT语句。