1

Is there any way to insert rows into a temporary table after creating it via SELECT statement in MySQL? I have this query that works as expected:

SELECT goldusers_user_ci.user_id, goldusers_users.username, goldusers_user_ci.contact_value
FROM goldusers_users
INNER JOIN goldusers_user_ci
ON goldusers_users.id = goldusers_user_ci.user_id
ORDER BY user_id

The idea is to select this query as a temporary table and add one row to it, like so:

SELECT * FROM(
SELECT goldusers_user_ci.user_id, goldusers_users.username, goldusers_user_ci.contact_value
FROM goldusers_users
INNER JOIN goldusers_user_ci
ON goldusers_users.id = goldusers_user_ci.user_id
ORDER BY user_id
) AS DBusers
INSERT INTO DBusers(user_id,username)
VALUES(-1,"All")

However, I get a syntax error. Is there any way to do this?

**Edit: I believe I actually need to do a union after the select: ***the issue is the placement of the ORDER BY statement. Moving it to the bottom works

SELECT goldusers_user_ci.user_id, goldusers_users.username, goldusers_user_ci.contact_value
FROM goldusers_users
INNER JOIN goldusers_user_ci
ON goldusers_users.id = goldusers_user_ci.user_id
UNION ALL
SELECT -1 as user_id, "All" as username, "" as contact_value
ORDER BY user_id
6
  • The syntax is INSERT INTO tablename (columns...) SELECT .... Commented Oct 21, 2021 at 19:06
  • See the documentation for INSERT Commented Oct 21, 2021 at 19:07
  • AS DBusers doesn't create a temporary table. Commented Oct 21, 2021 at 19:17
  • @Barmar Yes, maybe I do not need a temporary table. I am looking to add one row to the query after the select statements. Commented Oct 21, 2021 at 19:29
  • Then use UNION. Commented Oct 21, 2021 at 19:30

2 Answers 2

0

How about something like this?

SELECT * 
INTO #TempTable
FROM(
        SELECT goldusers_user_ci.user_id, goldusers_users.username, goldusers_user_ci.contact_value
        FROM goldusers_users
        INNER JOIN goldusers_user_ci
        ON goldusers_users.id = goldusers_user_ci.user_id
        
        UNION ALL
        
        SELECT -1, 'All', ''
) AS DBusers

SELECT *
FROM #TempTable

DROP TABLE #TempTable
Sign up to request clarification or add additional context in comments.

4 Comments

MySQL doesn't use # before temporary table names.
yeah my bad. Create a table first, replace #TempTable with the table.
MySQL doesn't use FROM in INSERT INTO statements.
It seems like you're giving the SQL-Server answer.
0

I cannot test what you want, but base on your query, I think you want to Insert data after select data.

SELECT * FROM(
SELECT goldusers_user_ci.user_id, goldusers_users.username, goldusers_user_ci.contact_value
FROM goldusers_users
INNER JOIN goldusers_user_ci
ON goldusers_users.id = goldusers_user_ci.user_id
ORDER BY user_id
) AS DBusers;
INSERT INTO DBusers(user_id,username)
VALUES(-1,"All");

I think you just need to add semicolon ;

2 Comments

The first query above works without the semicolon, but adding the semicolon to the second does not solve the issue.
@marksy95 The semicolon is needed to show where the first query ends and the second query starts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.