0

我有一个 MySQL 查询,它基本上获取所有信息来填充一个新表,但我不知道该怎么做:

  1. 创建一个新表,但如果它已经存在,则删除现有表并重新创建
  2. 将所有数据放入新表中

并希望有人能帮我一把或指出我正确的方向。

这是我现有的 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

4

1 回答 1

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服务器不支持。

阅读关于此
INSERT INTO...SELECT

于 2012-08-10T08:21:58.083 回答