Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:55:01+00:00 2026-06-09T13:55:01+00:00

I have a MySQL query which basically gets all the info to populate a

  • 0

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:

  1. Creating a new table but if it already exists then delete existing one and create again
  2. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:55:02+00:00Added an answer on June 9, 2026 at 1:55 pm

    Try INSERT INTO...SELECT statement

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

    From the MySQL Doc, SELECT INTO is not supported by the server.

    Read About This
    INSERT INTO…SELECT

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql query which selects 18 items from the table, but I'd
Okay, basically, I have formed a mySQL query which returns a data set all
I have one mysql query, which gets rows to designs list. In this list
I have a query which works fine in MySQL, but when I run it
Hi I have a query which is very straightforward to write in mysql but
basically i need to write a query for mysql, but i have no experience
I have a table in my MySQL DB which basically contains cron-like tasks. Basically
I have the following mysql query which I am running with php like so.
I have a complicated MySQL query which takes a lot of time, selecting from
I have the following mysql query, which is producing a list of entries by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.