我真的要创建临时表吗?

时间:2012-03-07 15:46:59

标签: mysql

只是想知道是否有人知道在不创建和删除表的情况下执行以下操作的更简洁方法?

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;

2 个答案:

答案 0 :(得分:3)

您可以在更新声明中进行联接。

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;

答案 1 :(得分:0)

只需将您的临时表选择语句移动到WHERE子句中吗?

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;
);