前言
之前一直沒有接觸數(shù)據(jù)庫的學習,只是本科時候修了一本數(shù)據(jù)庫基本知識的課。當時只對C++感興趣,天真的認為其它的課都沒有用,數(shù)據(jù)庫也是半懂不懂,胡亂就考試過了。現(xiàn)在學習大數(shù)據(jù)分析,接觸了數(shù)據(jù)挖掘,才感覺到數(shù)據(jù)庫是不可跨越的坎。直到現(xiàn)在才感覺到《操作系統(tǒng)》、《編譯原理》、《計算機組成原理》等等課程的重要性。在浩瀚的知識面前,個人是非常渺小的。掌握了一種思想之后,任何事情都不困難,困難的是你是否真的靜下心看一看幫助文檔、認真的Google。靜心、靜氣、認真、執(zhí)著。
游標-cursor
學習了幾天MySQL,談一談自己對游標的認識。
游標就類似于C++中的指針,用于指向查詢結(jié)果。比如你查詢后的的數(shù)據(jù)格式如下:
+ -- ----------+----------------------+------+------+------+-------------+----------+----------+------+---------+ | station_id | get_time | PM25 | PM10 | NO2 | temperature | pressure | humidity | wind | weather | + -- ----------+----------------------+------+------+------+-------------+----------+----------+------+---------+ | 001001 | 2 / 8 / 2013 9 : 00 : 00 PM | 149 | 59 | 16 | - 5 | 1031 | 46 | 4 | 1 | | 001001 | 2 / 8 / 2013 10 : 00 : 00 PM | 159 | 65 | 22 | - 5 | 1030 | 46 | 1 | 1 | | 001001 | 2 / 9 / 2013 12 : 00 : 00 AM | 179 | 73 | 28 | - 6 | 1029 | 46 | 4 | 1 | | 001001 | 2 / 9 / 2013 2 : 00 : 00 AM | 194 | 73 | 29 | - 7 | 1028 | 49 | 3 | 1 | | 001001 | 2 / 9 / 2013 3 : 00 : 00 AM | 191 | 73 | 27 | - 7 | 1028 | 50 | 2 | 1 | | 001001 | 2 / 9 / 2013 4 : 00 : 00 AM | 194 | 73 | 25 | - 7 | 1026 | 53 | 2 | 1 | | 001001 | 2 / 9 / 2013 5 : 00 : 00 AM | 193 | 73 | 23 | - 7 | 1026 | 54 | 2 | 1 | | 001001 | 2 / 9 / 2013 6 : 00 : 00 AM | 192 | 73 | 21 | - 8 | 1026 | 52 | 2 | 1 | | 001001 | 2 / 9 / 2013 7 : 00 : 00 AM | 192 | 73 | 23 | - 8 | 1025 | 54 | 3 | 1 | | 001001 | 2 / 9 / 2013 8 : 00 : 00 AM | 190 | 73 | 20 | - 8 | 1025 | 55 | 3 | 1 | + -- ----------+----------------------+------+------+------+-------------+----------+----------+------+---------+
你如果想逐條處理數(shù)據(jù),那么必須要用到游標進行循環(huán)處理。
加載進來的數(shù)據(jù)是varchar格式,但是對于第二個屬性“get_time”我們需要的格式是“datatime”,需要進行獲取屬性值并進行循環(huán)處理。
使用游標的步驟如下:
1.定義游標 declare 游標名 cursor for select語句
2.定義處理游標結(jié)束的變量?declare continue handler for not found ?set 變量名= true;
3.打開游標 open 游標名
4.判斷是否結(jié)束,如果不結(jié)束,則處理當前游標指向值;如果結(jié)束,則結(jié)束循環(huán)
5.關(guān)閉游標 close 游標名
注:游標一般是在存儲過程(procedure)中調(diào)用,procedure類似于C++中的函數(shù),里面封裝了SQL語句,想要使用時,直接CALL ‘procedure_name’即可。游標(cursor)中若有使用的變量必須在聲明cursor前把變量定義完。詳細的代碼設(shè)計如下:
CREATE DEFINER = `root`@`localhost` PROCEDURE `strToDate`() begin -- 定義一個臨時變量用于存儲轉(zhuǎn)換后的時間格式 declare temp datetime ; -- 定義字符串臨時變量,存儲查詢后的每條內(nèi)容 declare str varchar ( 150 ); -- 是否結(jié)束的標識 declare flag int default false; -- 定義游標 declare getTimeCursor cursor for select get_time from train; -- 定義結(jié)束的標識 declare continue handler for not found set flag = true; -- 打開游標 open getTimeCursor; -- 開始循環(huán)處理 read_loop:loop -- 把當前游標內(nèi)容放到變量中 fetch getTimeCursor into str ; -- 如果結(jié)束標識為TRUE,則結(jié)束循環(huán) if flag then leave read_loop; end if ; -- 否則循環(huán)處理每個屬性,調(diào)用字符串轉(zhuǎn)換日期函數(shù) set temp = ( select str_to_date( str , ' %c/%e/%Y %l:%i:%s %p ' )); -- 把轉(zhuǎn)換結(jié)果存儲到新的表中 insert into time_test values ( temp ); -- 結(jié)束循環(huán) end loop; -- 關(guān)閉游標 close getTimeCursor; -- 查詢結(jié)果 select * from time_test limit 10; end
其中,str_to_date()函數(shù)的功能是把string類型的數(shù)據(jù)轉(zhuǎn)成date類型。查詢后的結(jié)果為:
+ -- -------------------+ | get_time | + -- -------------------+ | 2013 - 02 - 09 16 : 00 : 00 | | 2013 - 02 - 08 21 : 00 : 00 | | 2013 - 02 - 08 22 : 00 : 00 | | 2013 - 02 - 09 00 : 00 : 00 | | 2013 - 02 - 09 02 : 00 : 00 | | 2013 - 02 - 09 03 : 00 : 00 | | 2013 - 02 - 09 04 : 00 : 00 | | 2013 - 02 - 09 05 : 00 : 00 | | 2013 - 02 - 09 06 : 00 : 00 | | 2013 - 02 - 09 07 : 00 : 00 | + -- -------------------+
see,所有字符串都轉(zhuǎn)換成了標準的時間格式。
MySQL load data控制
其實上面的問題完全可以利用另外一種方法完成,那就是在裝載數(shù)據(jù)的時候進行格式控制。具體SQL代碼如下:
use train; drop table traindata; create table if not exists traindata( id int auto_increment primary key , station_id varchar ( 10 ), get_time datetime , PM25 int , PM10 int , NO2 int , temperature int , pressure int , humidity int , wind double , weather int ); load data local infile ' f:\\dataset\\beijing\\crawleddata.txt ' into table traindata fields terminated by ' , ' (station_id, @var_time , PM25, PM10, NO2, temperature, pressure, humidity, wind, weather) set get_time = str_to_date( @var_time , ' %c/%e/%Y %l:%i:%s %p ' ); select * from traindata limit 10 ;
加載進數(shù)據(jù)庫后,具體數(shù)據(jù)格式如下:
+ -- --+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+ | id | station_id | get_time | PM25 | PM10 | NO2 | temperature | pressure | humidity | wind | weather | + -- --+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+ | 1 | 1001 | 2013 - 02 - 08 21 : 00 : 00 | 149 | 59 | 16 | - 5 | 1031 | 46 | 4 | 1 | | 2 | 1001 | 2013 - 02 - 08 22 : 00 : 00 | 159 | 65 | 22 | - 5 | 1030 | 46 | 1 | 1 | | 3 | 1001 | 2013 - 02 - 09 00 : 00 : 00 | 179 | 73 | 28 | - 6 | 1029 | 46 | 4 | 1 | | 4 | 1001 | 2013 - 02 - 09 02 : 00 : 00 | 194 | 73 | 29 | - 7 | 1028 | 49 | 3 | 1 | | 5 | 1001 | 2013 - 02 - 09 03 : 00 : 00 | 191 | 73 | 27 | - 7 | 1028 | 50 | 2 | 1 | | 6 | 1001 | 2013 - 02 - 09 04 : 00 : 00 | 194 | 73 | 25 | - 7 | 1026 | 53 | 2 | 1 | | 7 | 1001 | 2013 - 02 - 09 05 : 00 : 00 | 193 | 73 | 23 | - 7 | 1026 | 54 | 2 | 1 | | 8 | 1001 | 2013 - 02 - 09 06 : 00 : 00 | 192 | 73 | 21 | - 8 | 1026 | 52 | 2 | 1 | | 9 | 1001 | 2013 - 02 - 09 07 : 00 : 00 | 192 | 73 | 23 | - 8 | 1025 | 54 | 3 | 1 | | 10 | 1001 | 2013 - 02 - 09 08 : 00 : 00 | 190 | 73 | 20 | - 8 | 1025 | 55 | 3 | 1 | + -- --+------------+---------------------+------+------+------+-------------+----------+----------+------+---------+
see,標準的數(shù)據(jù)格式。
?
原創(chuàng)內(nèi)容,轉(zhuǎn)載請注明出處。http://www.cnblogs.com/chuantingSDU/p/4243990.html
聯(lián)系方式:chuanting.zhang@gmail.com
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
