日期:2014-05-16  浏览次数:20821 次

mysql临时表插入数据问题
mysql 5.1版本
临时表A,临时表B ,结构一样
insert into A(C1,C2)  
select C1,C2 from B limit 10;--- limit 10 报错。

请教,怎么改才能limit呢?在线等解答


------解决方案--------------------
create table c as select C1,C2 from B limit 10;
insert into A(C1,C2) select * from c;
------解决方案--------------------
insert into A(C1,C2) select * from (select C1,C2 from B limit 10)
------解决方案--------------------
SQL code
insert into A(C1,C2) select * from (select C1,C2 from B limit 10) r

------解决方案--------------------
INSERT INTO A SELECT * FROM (SELECT C1,C2 FROM B limit 10)t;

------解决方案--------------------
mysql> select * from tb_1;
+------+--------+----------+
| id | userid | friendid |
+------+--------+----------+
| 0 | 1 | 3 |
| 1 | 1 | 4 |
| 2 | 2 | 3 |
| 3 | 2 | 6 |
| 4 | 3 | 5 |
| 5 | 3 | 6 |
| 6 | 4 | 5 |
| 7 | 4 | 2 |
| 8 | 5 | 2 |
| 9 | 5 | 7 |
| 10 | 6 | 7 |
| 11 | 6 | 8 |
+------+--------+----------+
12 rows in set (0.04 sec)

mysql> create table tb_2 like tb_1;
Query OK, 0 rows affected (0.18 sec)

mysql> insert into tb_2 select * from tb_1 limit 5;
Query OK, 5 rows affected (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 0

我这个是可以的。