我有一个 MySQL 查询,它基本上获取所有信息来填充一个新表,但我不知道该怎么做:
- 创建一个新表,但如果它已经存在,则删除现有表并重新创建
- 将所有数据放入新表中
并希望有人能帮我一把或指出我正确的方向。
这是我现有的 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