Note that there are some explanatory texts on larger screens.

plurals
  1. POPlace result of query into new table
    primarykey
    data
    text
    <p>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:</p> <ol> <li>Creating a new table but if it already exists then delete existing one and create again</li> <li>Place all the data into the new table</li> </ol> <p>and was hoping somebody could give me a hand or point me in the right direction.</p> <p>Here is my existing SQL just in case it helps(Placed inside a PHP file):</p> <pre><code>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 &amp; ' ' 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 &amp; ' ' 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 &amp; ' ' 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 &amp; ' ' 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 &amp; ' ' 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 &amp; ' ' 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 </code></pre> <p>I tried using the format:</p> <pre><code>SELECT * INTO new_table_name FROM [query] </code></pre> <p>on the above query, but it just displays <code>Invalid query: Undeclared variable: new_table_name</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload