MySQL MySQL使用auto_increment id创建临时表并选择查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9021293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL create temporary table with auto_increment id and select query
提问by user1171669
want to create a temporary table that has an auto_increment field plus a field that has to be select from another table.
想创建一个临时表,它有一个 auto_increment 字段和一个必须从另一个表中选择的字段。
Here is what I have (does not work)
这是我所拥有的(不起作用)
CREATE TEMPORARY TABLE tmp (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
(SELECT valueName AS valueName
FROM sometable
WHERE sometable.somevalue='00'));
these work by them selves but can get the right syntax to do both
这些由他们自己工作,但可以获得正确的语法来完成这两项工作
CREATE TEMPORARY TABLE tmp (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
CREATE TEMPORARY TABLE tmp AS SELECT valueName AS valueName FROM sometable
WHERE sometable.somevalue='00';
回答by Andrew
I think you might be trying to do the first case outlined here:
我认为您可能正在尝试执行此处概述的第一个案例:
http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html
http://dev.mysql.com/doc/refman/5.5/en/create-table-select.html
..which for your example would look like:
..对于您的示例,它看起来像:
CREATE TEMPORARY TABLE tmp (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY)
SELECT valueName AS valueName FROM sometable
WHERE sometable.somevalue='00';
..so it might just be the parens in the wrong places that bit you in your first try.
..所以它可能只是在你第一次尝试时咬你的错误位置的括号。
回答by Marc B
CREATE TEMPORARY TABLE tmp (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
valueName varchar(16) // whatever type it should be
);
INSERT INTO tmp (valueName) SELECT valueName FROM sometable WHERE ...
Relevant docs here: http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html
相关文档在这里:http: //dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html