I have a MySQL query which basically gets all the info to populate a new table but I am not sure how to go about:
- Creating a new table but if it already exists then delete existing one and create again
- Place all the data into the new table
and was hoping somebody could give me a hand or point me in the right direction.
Here is my existing SQL just in case it helps(Placed inside a PHP file):
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
I tried using the format:
SELECT *
INTO new_table_name
FROM [query]
on the above query, but it just displays Invalid query: Undeclared variable: new_table_name
Try
INSERT INTO...SELECTstatementFrom the MySQL Doc,
SELECT INTOis not supported by the server.Read About This
INSERT INTO…SELECT