mysql的个人笔记

根据某字段的枚举值排序,例如:

SELECT * FROM `t_advertisement` ORDER BY FIELD(ad_status,'WAITING','RELEASE','REMOVE'),id desc;

获取上一条insert语句执行后的自增主键ID,作为下一条SQL的的字段值

INSERT INTO `database_name`.`table_name` (column_name,...,column_name)
values (column_value,...,column_value);

set @resourceId = @@identity;

INSERT INTO `database_name`.`table_name` (column_name,...,column_name)
select @resourceId ,column_value,...,column_value;

自定义函数,例如:

DELIMITER $$
CREATE FUNCTION `fun_sync_rule` () RETURNS INT (11)
BEGIN

DECLARE x INT ; 
INSERT INTO t_maintain_rule (
    model_id,
    mileage,
    months,
    create_time,
    update_time,
    create_by,
    update_by
) SELECT
    t1.id,
    5000,
    3,
    NOW(),
    NOW(),
    'system',
    'system'
FROM
    t_vehicle_model t1
LEFT JOIN t_maintain_rule t2 ON t1.id = t2.model_id
WHERE
    (
        t2.model_id IS NULL
        OR t2.model_id = 0
    ) ; 
SELECT ROW_COUNT() INTO x ; 
RETURN x ; 
END$$

DELIMITER

用存储过程迁移日志表记录,迁移7天的日志记录,例如:

DELIMITER $$
CREATE PROCEDURE `migrateOpLog`(OUT insertAffectRows INT(11),OUT deleteAffectRows INT(11))
BEGIN

SET @sql_drop_table = CONCAT("DROP TABLE IF EXISTS sys_op_log_history_",DATE_FORMAT(CURDATE()- INTERVAL 8 DAY,'%y%m%d'));
  PREPARE sql_drop_table FROM @sql_drop_table;
  EXECUTE sql_drop_table;

  SET @sql_create_table = CONCAT(
        "CREATE TABLE IF NOT EXISTS sys_op_log_history_",DATE_FORMAT(CURDATE()- INTERVAL 8 DAY,'%y%m%d'),
    "(
         `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '系统操作日志ID',
     `username` varchar(30) NOT NULL DEFAULT '' COMMENT '用户名',
     `module_name` varchar(50) NOT NULL DEFAULT '' COMMENT '操作模块',
     `org_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '机构id',
     `org_type` enum('FACTORY','REGION','DEALER','SERVICE_STATION','CUSTOMER') DEFAULT NULL,
     `op_content` varchar(1024) NOT NULL DEFAULT '' COMMENT '操作内容',
     `op_type` enum('ADD','UPDATE','DELETE','UPLOAD','DOWNLOAD','IMPORT','EXPORT') NOT NULL COMMENT '操作类型(新增,修改,删除,上传,下载,导入,导出)',
     `ip` varchar(36) NOT NULL DEFAULT '' COMMENT 'ip地址',
     `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
     `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
     `create_by` varchar(100) NOT NULL COMMENT '创建用户',
     `update_by` varchar(100) NOT NULL COMMENT '修改用户',
     PRIMARY KEY (`id`)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8");

  PREPARE sql_create_table FROM @sql_create_table;
  EXECUTE sql_create_table;

  SET @sql_op_record = CONCAT(
  "INSERT INTO sys_op_log_history_",DATE_FORMAT(CURDATE()- INTERVAL 8 DAY,'%y%m%d'),
   "(`id`,`username`,`module_name`,`org_id`,`org_type`,`op_content`,`op_type`,`ip`,`create_time`,`update_time`,`create_by`,`update_by`) ",
   "SELECT `id`,`username`,`module_name`,`org_id`,`org_type`,`op_content`,`op_type`,`ip`,`create_time`,`update_time`,`create_by`,`update_by` FROM sys_op_log where `create_time` < ",
   "CURDATE() - INTERVAL 7 DAY");

  PREPARE sql_op_record FROM @sql_op_record;
  EXECUTE sql_op_record;

  SELECT ROW_COUNT() INTO insertAffectRows;

  DELETE FROM sys_op_log WHERE `create_time` < CURDATE()- INTERVAL 7 DAY;

  SELECT ROW_COUNT() INTO deleteAffectRows;

END$$

DELIMITER

 

你可能感兴趣的:(mysql)