简体   繁体   English

ALTER TABLE ADD COLUMN和自动列列表

[英]ALTER TABLE ADD COLUMN and automatic COLUMNS list

I am looking to add a new column to table using the suggestions provided here and here 我正在寻找使用此处此处提供的建议表中添加新列

In essence, I would like the (fields_in_orig_table) to be populated automatically, and not having to enter them manually (have many columns and this changes from table to table): 本质上,我希望自动填充(fields_in_orig_table) ,而不必手动输入(有很多列,并且表与表之间的变化):

CREATE TABLE games_new LIKE games_orig;
ALTER TABLE games_new ADD COLUMN location varchar(256);
INSERT INTO games_new (fields_in_orig_table) SELECT * FROM games_orig;
RENAME TABLE games_orig TO games_old, games_new TO games_orig;
DROP TABLE games_old;

My thought goes around this: 我的想法是这样的:

CREATE TABLE games_new LIKE games_orig;
ALTER TABLE  games_new ADD COLUMN version varchar(256);
INSERT INTO  games_new 

          (SELECT CONCAT(GROUP_CONCAT(column_name ORDER BY ordinal_position 
           SEPARATOR " ,  "), " ") AS columns
           FROM information_schema.columns
           WHERE table_schema = 'games' AND table_name = 'games_orig' ) 

SELECT * FROM games_orig;
RENAME TABLE games_orig TO games_old, games_new TO games_orig;
DROP TABLE games_old;

This gives me syntax error (near the Select concat....). 这给了我语法错误(在Select concat ....附近)。

The original syntax to get comma delimited column listings is: 获取以逗号分隔的列列表的原始语法为:

SELECT CONCAT("'", GROUP_CONCAT(column_name ORDER BY ordinal_position SEPARATOR "', '"), "'") AS columns
FROM information_schema.columns
WHERE table_schema = 'db_name' AND table_name = 'tbl_name'

In my query, I have removed the extra quotes, as I figure my query does not require quotes as part of the column listing. 在查询中,我删除了多余的引号,因为我认为查询不需要引号作为列列表的一部分。

What am I doing wrong here? 我在这做错了什么? Who could help, please? 请问谁能帮忙?

INSERT INTO  games_new 

VALUES (SELECT CONCAT....

When inserting values in the table use INSERT INTO tablename VALUES (fields) instead of INSERT INTO tablename SET(fields) . 在表中插入值时,请使用INSERT INTO tablename VALUES (fields)而不是INSERT INTO tablename SET(fields)

CREATE TABLE games_new LIKE games_orig;
ALTER TABLE  games_new ADD COLUMN version varchar(256);
INSERT INTO  games_new 
          VALUES
          (SELECT CONCAT(GROUP_CONCAT(column_name ORDER BY ordinal_position 
           SEPARATOR ' , '), ' ') AS columns
           FROM information_schema.columns
           WHERE table_schema = 'games' AND table_name = 'games_orig' ) 

SELECT * FROM games_orig;
RENAME TABLE games_orig TO games_old, games_new TO games_orig;
DROP TABLE games_old;

You haven't specified which column you want to insert into, because your nested query is returning only 1 value 您尚未指定要插入的列,因为嵌套查询仅返回1个值

INSERT INTO games_new (column_name_u_want_to_insert_value_into) 
SELECT   cast(concat(group_concat(column_name ORDER BY ordinal_position SEPARATOR " , "), " ") AS CHAR) AS columns
FROM     information_schema.columns 
WHERE    table_name = 'games_orig';

also, if you are running all the statements together, add semicolon(;) for the insert query as well 另外,如果同时运行所有语句,请为插入查询添加分号(;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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