将查询结果放入新表中

时间:2012-08-10 08:19:44

标签: mysql database

我有一个MySQL查询,它基本上获取了填充新表的所有信息,但我不知道该如何进行:

  1. 创建新表但如果已存在,则删除现有表并再次创建
  2. 将所有数据放入新表格
  3. 并且希望有人能帮助我或指出我正确的方向。

    这是我现有的SQL,以防它有帮助(放在PHP文件中):

    SELECT
     * 
    FROM (                                                                                                                                                                                                                                                                                                                                                                         
    SELECT
        'OverDue' AS ParentNode,
        'Documents ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '2' AS GroupLevel,
        'Red' AS ImageNamePrefix         
    FROM 
        documents         
    WHERE
        datenextreview BETWEEN '$datefrom' AND '$dateto'
    
    UNION ALL                    
    SELECT
        'Documents ' AS ParentNode,
        InternalorExternal & ' ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '3' AS GroupLevel,
        'Red' AS ImageNamePrefix         
    FROM
        documents         
    WHERE
        datenextreview BETWEEN '$datefrom' AND '$dateto'          
    GROUP BY InternalorExternal                     
    
    UNION ALL                    
    SELECT
        'OverDue' AS ParentNode,
        'Equipment ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '2' AS GroupLevel,
        'Red' AS ImageNamePrefix         
    FROM
        equipment
    WHERE
        datenextcalib BETWEEN '$datefrom' AND '$dateto'
    
    UNION ALL                    
    SELECT
        'Equipment ' AS ParentNode,
        EqpmtType & ' ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '3' AS GroupLevel,
        'Red' AS ImageNamePrefix         
    FROM
        equipment
    WHERE
        datenextcalib BETWEEN '$datefrom' AND '$dateto'
    GROUP BY EqpmtType
    
    UNION ALL                                                                                                                                                                                                                                                                                                                                                       
    
    
    SELECT
        'Due' AS ParentNode,
        'Documents ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '2' AS GroupLevel,
        'Yellow' AS ImageNamePrefix         
    FROM documents         
    WHERE
            datenextreview BETWEEN '$datefrom' AND '$dateto'                 
    
    UNION ALL                    
    SELECT
        'Documents ' AS ParentNode,
        InternalorExternal & ' ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '3' AS GroupLevel,
        'Yellow' AS ImageNamePrefix         
    FROM documents         
    WHERE
        datenextreview BETWEEN '$datefrom' AND '$dateto'          
    GROUP BY InternalorExternal
    
    UNION ALL                    
    SELECT
        'Due' AS ParentNode,
        'Equipment ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '2' AS GroupLevel,
        'Yellow' AS ImageNamePrefix         
    FROM equipment
    WHERE
        datenextcalib BETWEEN '$datefrom' AND '$dateto'
    
    UNION ALL                    
    SELECT
        'Equipment ' AS ParentNode,
        EqpmtType & ' ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '3' AS GroupLevel,
        'Yellow' AS ImageNamePrefix         
    FROM equipment         
    WHERE
        datenextcalib BETWEEN '$datefrom' AND '$dateto'        
    GROUP BY EqpmtType
    
    UNION ALL                                                                                                                                                                                                                                                                                                                                                       
    
    
    SELECT
        'Coming Up' AS ParentNode,
        'Documents ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '2' AS GroupLevel,
        'Green' AS ImageNamePrefix
    FROM  documents        
    WHERE
        datenextreview BETWEEN '$datefrom' AND '$dateto'
    
    UNION ALL                    
    SELECT
        'Documents ' AS ParentNode,
        InternalorExternal &  ' ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '3' AS GroupLevel,
        'Green' AS ImageNamePrefix         
    FROM documents
    WHERE
        datenextreview BETWEEN '$datefrom' AND '$dateto'
    GROUP BY InternalorExternal
    
    UNION ALL                    
    SELECT
        'Coming Up' AS ParentNode,
        'Equipment ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '2' AS GroupLevel,
        'Green' AS ImageNamePrefix         
    FROM  equipment
    WHERE
        datenextcalib BETWEEN '$datefrom' AND '$dateto'
    
    UNION ALL                    
    SELECT
        'Equipment ' AS ParentNode,
        EqpmtType &   ' ' AS ChildNode,
        COUNT(*) AS NoOfFails,
        '3' AS GroupLevel,
        'Green' AS ImageNamePrefix         
    FROM equipment
    WHERE
        datenextcalib BETWEEN '$datefrom' AND '$dateto'
    GROUP BY EqpmtType
    ) AS table2              
    ORDER BY         
     GroupLevel,
     ChildNode DESC,
     ParentNode DESC
    

    我尝试使用格式:

    SELECT *
    INTO new_table_name
    FROM [query]
    

    在上面的查询中,但它只显示Invalid query: Undeclared variable: new_table_name

1 个答案:

答案 0 :(得分:2)

尝试INSERT INTO...SELECT声明

INSERT INTO newTable (ChildNode, NoOfFails, GroupLevel, ImageNamePrefix)
SELECT ... -- paste your SELECT STATEMENT HERE (the one you posted)

从MySQL Doc,服务器不支持SELECT INTO

Read About This
INSERT INTO...SELECT