mysql 学习
1 #----------------------------------- 2 #1.正则表达式检索 [RegexP] 3 #----------------------------------- 4 # * Binary字段可以支持大小写匹配 5 # * 文本开始^(集合中是否定) 结束$ 6 select * from goods where bid RegexP Binary 'E|a' # * | 为或 7 select * from goods where bid RegexP Binary '[123]Ton' # * [123]Ton为匹配 1Ton 2Ton 3Ton 8 select * from goods where bid RegexP Binary '[1-3]Ton' * 同上 9 select * from goods where bid RegexP Binary '[^123]Ton' # * [123]Ton为不匹配 1Ton 2Ton 3Ton 10 select * from goods where bid RegexP Binary '\\.' # * 匹配特殊字符需要\\转义 11 * 也可以匹配元字符, \\f \\n \\r \\t \\v 12 13 select 'justalnums' REGEXP '[[:alpha:]]'; # * [:alpha:] 为任意字符(同[a-zA-Z]) 14 * 其他character class: 15 alnum,blank,cntrl,digit,graph,lower,print,punct,space,upper,xdigit 16 17 select '***' RegexP '\\([0-9] sticks?)\\' # 18 select '***' RegexP '[[:digit:]]{4}' # 存在4位数字的 19 20 21 #----------------------------------- 22 #2.函数 23 #----------------------------------- 24 字符函数 25 * IFNull() = COALESCE() 26 时间函数 27 * now(),curdate(),curtime(),dayofweek(now()) *第一日是周日 28 * addDate(DataChange_CreateTime,-1),AddTime(DataChange_CreateTime,-1) 29 * Date(DataChange_CreateTime),year(DataChange_CreateTime),month(DataChange_CreateTime),day(DataChange_CreateTime) 30 * time(DataChange_CreateTime),hour(DataChange_CreateTime),minute(DataChange_CreateTime),second(DataChange_CreateTime) 31 * DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p') 32 * date_add(DataChange_CreateTime, interval 1 DAY_MINUTE) * 增 33 * date_sub(DataChange_CreateTime, interval 1 DAY_MINUTE) * 减 34 35 * SELECT now(),unix_timestamp( now() ),from_unixtime(unix_timestamp( now() )) *UTC系统时间戳 与 当前时区timestamp 转换 36 37 数值函数 38 floor()向上取整 round()四舍五入 rand()随机数 ceil() ceiling()向下取整 39 40 41 #----------------------------------- 42 #3.游标 43 #----------------------------------- 44 delimiter // 45 CREATE PROCEDURE TestCursor() 46 BEGIN 47 DECLARE v_id INT; 48 DECLARE v_value VARCHAR(10); 49 50 -- 游标控制的标志 51 DECLARE no_more_departments INT; 52 53 -- 定义游标. 54 DECLARE c_test_main CURSOR 55 FOR 56 SELECT goodsid,cast(now() as CHAR) as value FROM goods; 57 58 -- 当游标没有数据的时候 59 -- 设置 no_more_departments = 1 60 DECLARE CONTINUE HANDLER 61 FOR 62 NOT FOUND SET no_more_departments=1; 63 64 -- 设置初始标志位,认为游标是有数据的. 65 SET no_more_departments=0; 66 67 -- 打开游标 68 OPEN c_test_main; 69 70 -- 获取游标数据 71 FETCH c_test_main INTO v_id, v_value; 72 73 -- 循环所有的行 74 WHILE no_more_departments = 0 DO 75 -- 输出调试信息 76 SELECT v_id, v_value; 77 -- 获取游标数据 78 FETCH c_test_main INTO v_id, v_value; 79 END WHILE; 80 81 -- 关闭游标 82 CLOSE c_test_main; 83 END 84 // 85 86 #drop procedure TestCursor; 87 CALL TestCursor();