[英]mysql - error with creating temporary table from resultset
i want to create a temporary table on the fly from a result set so that i can run some queries against this resultset. 我想从结果集中即时创建一个临时表,以便我可以对此结果集运行一些查询。
Here is my sql: 这是我的SQL:
CREATE TABLE temp_table_1 AS (SELECT * FROM A LEFT OUTER JOIN B ON A.id = B.id WHERE B.id IS null);
SELECT QUA.id, QUA.name, QUA.address, QUA.acc, QUA.module, QUA.tag
FROM QUA,temp_table_1
WHERE
QUA.name = temp_table_1.name AND
QUA.acc = temp_table_1.acc AND
QUA.tag = temp_table_1.tag
When i run the first query to create the temp table, I get the error message: 当我运行第一个查询以创建临时表时,出现错误消息:
'Duplicate Colum Name 'id'' '重复的列名'id'
Thanks a lot for your help. 非常感谢你的帮助。
You're getting both columns A.id
and B.id
without aliases to differentiate them. 您将同时获得A.id
和B.id
列,而没有别名以区分它们。 Be specific about the columns you select: 具体说明您选择的列:
CREATE TABLE temp_table_1 AS (
SELECT
A.id AS aid
A.othercol AS a_othercol,
B.id AS bid,
B.othercol AS b_othercol
FROM A LEFT OUTER JOIN B ON A.id = B.id WHERE B.id IS null);
It's unwise to use SELECT *
because you can't always guarantee the order that the columns will return in, if something changed in the original CREATE TABLE
statement, for example. 使用SELECT *
是不明智的,因为例如,如果原始CREATE TABLE
语句中发生了某些更改,您将无法始终保证列将返回的顺序。 Also, as you've discovered here, similarly named columns will cause problems in many circumstances. 另外,正如您在此处发现的那样,在许多情况下,名称相似的列也会引起问题。
THat's because you do SELECT *
. 这是因为您执行SELECT *
。 The resultset contains all columns from both tables, so if both of them have a column called 'id' it wil be duplicated. 结果集包含两个表中的所有列,因此,如果它们两个都有一个名为“ id”的列,则它将被复制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.