触发器:
触发器只能作用在表的增删改操作上
新增时触发器
begin
insert into txf_alterinfo
(uqalterid, alterbef, alterafter, uqfieldid, uqentityid, uquserid, operatedate, uqkeyid, varmemo)
values
(uuid(), '', new.varname, 'UQPROJECTID', 'TXF_PROJECT', new.create_user, NOW(), new.uqprojectid, '项目新增');
insert into txf_project_rensume
(id, uqprojectid, vartype, resume, altertime, create_time, create_user, update_time, update_user, `desc`)
values
(uuid(), new.uqprojectid,1, '项目初始录入',date_format(now(), '%Y-%m-%d'), NOW(),new.create_user,
NULL,NULL,'项目初始录入');
end
修改时触发器
begin
DECLARE oldvalue VARCHAR(1000);
DECLARE newvalue VARCHAR(1000);
DECLARE user_name VARCHAR(50) DEFAULT '';
IF(new.projectstate='20_2') then -- 双重if 结尾必须要有end if结束
if(IFNULL(new.varname,'') != IFNULL(old.varname,'')) then
insert into txf_alterinfo
(uqalterid, alterbef, alterafter, uqfieldid, uqentityid, uquserid, operatedate, uqkeyid, varmemo)
values
(uuid(), old.varname, new.varname, 'VARNAME', 'TXF_PROJECT', new.update_user, NOW(), NEW.uqprojectid, '项目名称变更');
insert into txf_project_rensume
(id, uqprojectid, vartype, resume, altertime, create_time, create_user, update_time, update_user, `desc`)
values
(uuid(), new.uqprojectid,8, CONCAT('项目名称变更为:', new.varname),date_format(now(), '%Y-%m-%d'), NOW(),old.create_user, NOW(),NEW.update_user,CONCAT( old.varname,'项目名称变更为:', new.varname));
end if;
end if;
end
mysql定义变量 与 变量赋值
DECLARE oldvalue VARCHAR(1000); -- 定义变量
DECLARE newvalue VARCHAR(1000);
SELECT a.dict_label INTO newvalue FROM sys_dict_data a WHERE dict_type='org_resume' and dict_value=1; -- 变量赋值
mysql拼接字符串 参数
CONCAT( oldvalue,"法人名称变更为:",newvalue)
mysql切割“,”字符串
SELECT substring_index(substring_index('1_1,1_2', ',', b.help_topic_id + 1), ',', -1) dictvalue
FROM mysql.help_topic b
mysql日期类型转换
select date_format(now(), '%Y-%m-%d') FROM dual
字符串数字排序
order by lpad(a.varno,5,'0') asc
mysql多字段模糊查询一个参数
select * from txf_project where CONCAT(`cyry`,`projectmanager`,`TECMANAGER`) like concat('%', '5db05328-bf94-4795-bb31-a4f4cd373b40', '%')
mysql字符串逗号分隔(行变列)
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX((select servicecontent from txf_project) AS num
FROM
mysql.help_topic
WHERE
help_topic_id < LENGTH((select servicecontent from txf_project))-
LENGTH(REPLACE((select servicecontent from txf_project ),',',''))+1
mysql子查询 FIND_IN_SET
select a.* from sys_dict_data a where FIND_IN_SET(a.dict_value,'6_7,6_6,6_5,6_4')
mysql逗号转换(字符转换)
-- 先转换 后拼接成想要的字符 但是不可用子查询 in
select servicecontent, (CONCAT("'",replace(servicecontent,',',"','"),"'")) FROM txf_project
备份一张表&&修改表明
create table txf_file20210118 as select * from txf_file20210117
ALTER TABLE TXF_FILE20210117 RENAME TO TXF_FILE
[Mysql中将查询出来的多列的值用逗号拼接] (列变行) 逗号分隔
select group_concat(字段名) from 表名
Mysql 替换某个字段中的某个字符
update txf_filemap set FILEPATH = REPLACE (FILEPATH,'c:','C:')
mysql时间戳转换
-- 时间戳 2018-08-09
SELECT CURDATE();
-- 2018-08-09 11:16:03
SELECT now();
-- 1618219395
select unix_timestamp(now())+10000;
-- 1533744000
select unix_timestamp(CURDATE());
select from_unixtime(unix_timestamp(now())+500, '%Y-%m-%d %H:%i:%s')
数据迁移(复制)
MySQL将一张表的某些列数据,复制到另外一张表
INSERT INTO t_topic_content(content,topicId) SELECT content,id FROM t_topic;
注意:给某一列数据赋值,自增长,那么就不应该插入数据了
MySQL将一张表的某些列数据,复制到另外一张表,并且修改某些内容。方法同上, 只是查询的时候使用REPLACE(str,from_str,to_str) 函数
INSERT INTO t_topic_content(content,topicId)
SELECT REPLACE(content, 'aa', 'bb'),id FROM t_topic
MySQL分页插件 PageHelper自定义Count查询
<select id="selectLeftjoin" resultType="com.github.pagehelper.model.User">
select a.id,b.name,a.py from user a
left join user b on a.id = b.id
order by a.id
</select>
<select id="selectLeftjoin_COUNT" resultType="Long">
select count(distinct a.id) from user a
left join user b on a.id = b.id
</select>
mysql 只满足一个条件的 in 查询 避免扫描全表
原理:前面为主表 只需要把 in 后面的sql 换成下方字段即可
not exists (select 1 from txf_zhiye_tuisong_log tl where tl.zhiyetimeid = a.id)
mysql替换字符集
ALTER TABLE txf_homepageinfo convert to CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
mysql一对多查询最新一条数据
查询最近的一条执业反馈单
select * from txf_zhiye where create_time in (select MAX(create_time) from txf_zhiye GROUP BY uqprojectid) and zhiyetype=1