表如下所示
代碼如下 復制代碼SELECT * FROM `test_table_draw` WHERE `borough_id`>=10266 and `borough_id`
<=12354
戶型表
SELECT * FROM `test_table_pic` WHERE `borough_id`>=10266 and `borough_id`
<=12354
相冊表
創建一個備份表
代碼如下 復制代碼CREATE TABLE IF NOT EXISTS `test_table_tp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pic_url` varchar(200) NOT NULL,
`pic_thumb` varchar(200) DEFAULT NULL,
`pic_desc` varchar(200) DEFAULT NULL,
`borough_id` int(11) NOT NULL,
`creater` varchar(20) DEFAULT NULL,
`addtime` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `borough_id` (`borough_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=97289 ;
語法
代碼如下 復制代碼Insert into Table2(field1,field2,...) select value1,value2,... from Table1
例子
Insert into test_table_tp select * from test_table_draw where `borough_id`>=10266 and `borough_id`
錯誤提示
代碼如下 復制代碼INSERT INTO test_table_tp
SELECT *
FROM test_table_draw
WHERE `borough_id` >=10266
AND `borough_id`
MySQL 返回:
#1136 - Column count doesn't match value count at row 1
解決
代碼如下 復制代碼Insert into(id,pic_url,pic_thumb,pic_desc,borough_id,creater,addtime) test_table_tp select id,pic_url,pic_thumb,pic_desc,borough_id,creater,addtimefrom test_table_draw where `borough_id`>=10266 and `borough_id`
<=12354
後來發現上面不對於時又找
代碼如下 復制代碼Select * Into new_table_name from old_table_name;
測試儀
Select * Into test_table_tp1 from test_table_draw where `borough_id`>=10266 and `borough_id` <=12354
執行後錯誤
錯誤
SQL 查詢:
SELECT *
INTO test_table_tp
FROM test_table_draw
WHERE `borough_id` >=10266
AND `borough_id`
LIMIT 0 , 30
MySQL 返回:
#1327 - Undeclared variable: test_table_tp
網上找到提示要求目標表Table2不存在,因為在插入時會自動創建表Table2,並將Table1中指定字段數據復制到Table2中。
代碼如下 復制代碼 Select * Into test_table_tp1 from test_table_draw where `borough_id`>=10266 and `borough_id` <=12354
gg查一下發現原來 mysql 數據庫是不支持 SELECT INTO FROM 這種語句的,但是經過研究是可以通過另外一種變通的方法解決這個問題的
代碼如下 復制代碼 Create table Table2 (Select * from Table1);
這種語句代替
代碼如下 復制代碼
SELECT vale1, value2, value3 into Table2 from Table1;
下面看一個本人實際操作中的例子吧!
代碼如下 復制代碼
Create table www.111cn.net (Select A.id,B.id as typeId,A.brand,A.quanpin,A.simple from brands as A,cartype as B where A.type=B.name group by A.brand);
這個是關聯兩個表得出一個查詢結果,然後將結果插入到一個新創建的表 www.111cn.net 中。
好了現在我再來做
代碼如下 復制代碼
Create table pic_test (Select * from test_table_draw where `borough_id`>=10266 and `borough_id` <=12354)
成功了啊
您運行的 SQL 語句已經成功運行了。 (查詢花費 0.5645 秒)
SQL 查詢: