mysql字符串拼接,存储过程
添加字段:
alter table `user_movement_log`
Add column GatewayId int not null default 0 AFTER `Regionid` (在哪个字段后面添加)
MySQL concat函数可以连接一个或者多个字符串,如
select concat('10');
结果:10
select concat('11','22','33');
结果
112233
MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
select concat('11','22',null);
结果
NULL
concat(b.yearnumber,Lpad(b.monthofyear,2,0))拼接字符串。
Lpad(4,2,0)结果:04,‘4’的位数是1位小于2两位,所以在4的前边补充0
LPAD(s1,len,s2)函数将字符串s2填充到s1的开始处,使字符串的长度达到len,然后返回字符串s1。如果字符串s1的长度大于len,则返回值被缩短至len字符长度。
Rpad(4,2,0)结果:40,‘4’的位数是1位小于2两位,所以在4的后边补充0
RPAD(s1,len,s2)函数将字符串s2填充到s1的结尾处,使字符串的长度达到len,然后返回字符串s1。如果字符串s1的长度大于len,则返回值被缩短至len字符长度。
存储过程实例:
DELIMITER $$
USE `ucmweb`$$
DROP PROCEDURE IF EXISTS `mmm_bi_data_process_proc`$$
CREATE PROCEDURE mmm_bi_data_process_proc()
BEGIN
UPDATE mmmm_process t
SET data_id=(SELECT id FROM mmm_date_time WHERE currdatevar=DATE_FORMAT(SYSDATE(),'%Y%m%d')-1),
use_count= (SELECT COUNT(*) FROM mmm_contact ),
merge_count=(SELECT COUNT(*) FROM mmm_reference_contact)
WHERE t.source_count IS NOT NULL AND t.use_count IS NULL AND t.me111_count IS NULL;
END$$
DELIMITER ;
USE `ucmweb`$$
DROP PROCEDURE IF EXISTS `mmm_bi_data_process_proc`$$
CREATE PROCEDURE mmm_bi_data_process_proc()
BEGIN
UPDATE mmmm_process t
SET data_id=(SELECT id FROM mmm_date_time WHERE currdatevar=DATE_FORMAT(SYSDATE(),'%Y%m%d')-1),
use_count= (SELECT COUNT(*) FROM mmm_contact ),
merge_count=(SELECT COUNT(*) FROM mmm_reference_contact)
WHERE t.source_count IS NOT NULL AND t.use_count IS NULL AND t.me111_count IS NULL;
END$$
DELIMITER ;