MySQL 关联表批量修改(数据同步)
因为在代码中新建分配记录数据时忘记加入资源id。发现后,从操作记录表中获取对应的资源id,批量修改。
update xd_resource_distribution xdrd, xd_resource_operation xdro set xdrd.resources_id = xdro.resources_id where xdro.operation_records
like '分配了%' and xdrd.dis_time = xdro.operating_time;
MySQL判断是否有符合条件的数据
select 1 from tableName where col = 'col' limit 1;
MySQL 字符串拼接
CONCAT(string1,string2,…) 说明 : string1,string2代表字符串,concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL
CONCAT_WS(separator,str1,str2,...)
说明 : string1,string2代表字符串,concat_ws 代表 concat with separator,第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
group_concat(name)
说明:将多行转化为一行,多用于group by
mysql实现oracle的rownum功能
SELECT @rowno:=@rowno+1 as rowno,r.* from story r ,(select @rowno:=0) t;
mysql自定义函数
USE `mytest`;
DROP function IF EXISTS `new_function`;
DELIMITER $$
USE `mytest`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `new_function`() RETURNS text CHARSET utf8mb4
DETERMINISTIC
BEGIN
declare allNames text default '';
declare oneName varchar(45);
declare student_list cursor for select t.name from cai_student t;
declare continue handler for SQLSTATE '02000' SET oneName = null;
open student_list;
FETCH student_list INTO oneName;
while oneName is not null do
set allNames = CONCAT(allNames ,oneName,'_') ;
FETCH student_list INTO oneName;
end while;
close student_list;
RETURN allNames;
END$$
DELIMITER ;