在MySQL中,我想使用此查询中的所有信息创建一个新表:
select * into consultaa2 from SELECT CONCAT( 'UPDATE customers SET customers_default_address_id= ', (SELECT a.address_book_id FROM address_book a where c.customers_id=a.customers_id order by address_book_id desc limit 1), ' WHERE customers_id = ', customers_id, ';') AS sql_statement FROM customers c where c.customers_id > 3894;
该查询对于浏览器来说太长了,无法显示concat,因此我需要进行此更新。
请注意, 此方法不会 创建 表(按照OP标题)。为此,请参见以下答案。
您可以这样做:
CREATE TABLE tablename SELECT * FROM othertable;
tablename是要创建的新表的名称,SELECT * FROM othertable是返回应从中创建表的数据的查询。
插入具有查询信息的表的格式为
INSERT INTO <TABLE-1> SELECT * FROM <TABLE-2>
在您的情况下,
insert into consultaa2 SELECT CONCAT( 'UPDATE customers SET customers_default_address_id= ', (SELECT a.address_book_id FROM address_book a where c.customers_id=a.customers_id order by address_book_id desc limit 1), ' WHERE customers_id = ', customers_id, ';') AS sql_statement FROM customers c where c.customers_id > 3894;
只要确保要插入的表中的列与选择查询返回的列匹配即可。