简体   繁体   中英

Do I really have to create a temp table?

Just wondering if anyone knows a neater way of doing the following without creating and dropping a table?

CREATE TEMPORARY TABLE temp_table AS
SELECT table_one.col1 FROM table_one
JOIN table_two ON (table_two.col1 = table_one.col1 )
WHERE table_one.col2 = $arg1 AND table_two.col2= $arg2;

UPDATE table_two SET col3 = $arg3
WHERE col1 IN 
(
    SELECT col1 FROM temp_table
);

DROP TABLE temp_table;

You could do a join in update statement.

UPDATE table_two, table_one SET table_two.col3 = $arg3
WHERE table_two.col1=table_one.col1 AND table_one.col2 = $arg1 AND table_two.col2= $arg2;

Just move your Temporary Table select statement into your WHERE clause?

UPDATE table_two SET col3 = $arg3
WHERE col1 IN 
(
    SELECT table_one.col1 FROM table_one
    JOIN table_two ON (table_two.col1 = table_one.col1 )
    WHERE table_one.col2 = $arg1 AND table_two.col2= $arg2;
);

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