简体   繁体   中英

MySQL Temp table Insert

I'm using MySQL and trying to create a temp table. I will be doing a 2 while loop statements in PHP to populate the temp table. Firstly though I can't seem to get the Insert into temp table to work. I've tried many different versions of this, some using '#' for the table and various things (are there differences in SQL server and MySQL commands?). Here's my last attempt (PS the Select statement works fine on its own).

CREATE TEMPORARY TABLE temp
(
aID varchar(15) NOT NULL,
bID varchar(15) NOT NULL
)
INSERT INTO temp
SELECT aID, bID
FROM tags
WHERE placeID = "abc" AND tagID = "def";

Help appreciated!

Also, just a general Q...this query will have to be run many times. Will using temp tables be OK or cause the server issues?

You can create temporary table and insert select statemet in following way:

create temporary table temp
SELECT aID, bID
FROM tags
WHERE placeID = "abc" AND tagID = "def";

To drop the temporary table before creating it again. put following statement before creating temporary table:

drop temporary table if exists temp;

Note: It will be good if you can put all this code in stored procedure. and call it to create temporary table.

working on what Code-Monk wrote, consider the following:

drop procedure if exists uspK;
DELIMITER $$
create procedure uspK ()
BEGIN
    drop temporary table if exists temp; -- could be some other random structure residue

    create temporary table temp
    SELECT aID, bID
    FROM tags
    WHERE placeID = "abc" AND tagID = "def";

    -- use the temp table somehow
    -- ...
    -- ...
    -- ...

    drop temporary table temp; -- otherwise it survives the stored proc call
END
$$ -- signify end of block
DELIMITER ; -- reset to default delimiter

Test Stored Procedure

call uspK(); -- test it, no warnings on edge conditions

What not to do

One would not find much luck with the following. If you think so, run it a few times;

drop procedure if exists uspK;
DELIMITER $$
create procedure uspK ()
BEGIN
    -- drop temporary table if exists temp;

    create temporary table if not exists temp
    SELECT aID, bID
    FROM tags
    WHERE placeID = "abc" AND tagID = "def";

    -- use the temp table somehow
    -- ...
    -- ...
    -- ...

    -- drop temporary table temp; -- otherwise it survives the stored proc call
END
$$ -- signify end of block
DELIMITER ; -- reset to default delimiter

because create temporary table if not exists temp is flakey

General Comments

One should not embark into writing stored procs until somewhat fluent on the simple topic of DELIMITERS. Wrote about them in a section here called Delimiters . Just hoping to head you off from unnecessary wasted time on such a simple thing, than can waste a lot of debugging time.

Also, here in your question, as well as in that reference, keep in mind that the creation of tables is DDL that can have a large percentage of the overall profiling (performance). It slows down a proc versus using a pre-existing table. One might think the call is instantaneous, but it is not. As such, for performance, using a pre-existing table with ones results put into their own segmented rowId is much faster than enduring DDL overhead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM