Sai*_*ios 37 mysql sql insert sql-update
在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;
Run Code Online (Sandbox Code Playgroud)
查询太长,浏览器无法显示concat,我需要这个来进行更新.
Dan*_*rth 133
你可以这样做:
CREATE TABLE tablename SELECT * FROM othertable;
Run Code Online (Sandbox Code Playgroud)
tablename
是要创建的新表的名称,SELECT * FROM othertable
是返回应从中创建表的数据的查询.
Suk*_*mar 25
使用查询中的信息插入表格的格式
INSERT INTO <TABLE-1>
SELECT * FROM <TABLE-2>
Run Code Online (Sandbox Code Playgroud)
在你的情况下,它会
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;
Run Code Online (Sandbox Code Playgroud)
只需确保要插入的表中的列以及从select查询返回的列匹配.
Eri*_*ski 12
mysql创建新表
mysql命令行示例.
mysql> create table foo(id int, vorta text);
Query OK, 0 rows affected (0.02 sec)
Run Code Online (Sandbox Code Playgroud)
插入行
mysql> insert into foo values(1, 'for the hoarde');
Query OK, 1 row affected (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
看看那里有什么
mysql> select * from foo;
+------+----------------+
| id | vorta |
+------+----------------+
| 1 | for the horde |
+------+----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
使用查询中的信息创建一个新表
mysql> create table foo2 select * from foo;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
Run Code Online (Sandbox Code Playgroud)
检查数据是否移动
mysql> select * from foo2;
+------+----------------+
| id | vorta |
+------+----------------+
| 1 | for the horde |
+------+----------------+
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
82598 次 |
最近记录: |