MySQL:根据指定日期加365天,当前时间距离明年的指定的日期还有多少天-SQL
一、数据准备
/* Navicat Premium Data Transfer Source Server : songweipeng Source Server Type : MySQL Source Server Version : 50730 Source Host : localhost:3306 Source Schema : springboot-vue Target Server Type : MySQL Target Server Version : 50730 File Encoding : 65001 Date: 17/08/2022 14:59:45 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for sys_user -- ---------------------------- DROP TABLE IF EXISTS `sys_user`; CREATE TABLE `sys_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '用户名', `nick_name` varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称', `avatar` varchar(150) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '头像', `password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码', `salt` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '加密盐', `email` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱', `mobile` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号', `status` tinyint(4) NULL DEFAULT NULL COMMENT '状态 0:禁用 1:正常', `dept_id` bigint(20) NULL DEFAULT NULL COMMENT '机构ID', `create_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '创建人', `create_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间', `last_update_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '更新人', `last_update_time` datetime(0) NULL DEFAULT NULL COMMENT '更新时间', `del_flag` tinyint(4) NULL DEFAULT 0 COMMENT '是否删除 -1:已删除 0:正常', PRIMARY KEY (`id`) USING BTREE, UNIQUE INDEX `name`(`name`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 35 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '用户管理' ROW_FORMAT = Compact; -- ---------------------------- -- Records of sys_user -- ---------------------------- INSERT INTO `sys_user` VALUES (2, 'liubei', '刘备', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 7, 'admin', '2022-08-17 19:43:00', 'admin', '2019-01-10 11:41:13', 0); INSERT INTO `sys_user` VALUES (3, 'zhaoyun', '赵云', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 7, 'admin', '2022-08-17 19:43:44', 'admin', '2018-09-23 19:43:52', 0); INSERT INTO `sys_user` VALUES (4, 'zhugeliang', '诸葛亮', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 7, 11, 'admin', '2022-08-17 19:44:23', 'admin', '2018-09-23 19:44:29', 0); INSERT INTO `sys_user` VALUES (5, 'caocao', '曹操', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 8, 'admin', '2022-08-17 19:45:32', 'admin', '2019-01-10 17:59:14', 0); INSERT INTO `sys_user` VALUES (6, 'dianwei', '典韦', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 10, 'admin', '2022-08-17 19:45:48', 'admin', '2018-09-23 19:45:57', 0); INSERT INTO `sys_user` VALUES (7, 'xiahoudun', '夏侯惇', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 8, 'admin', '2022-08-17 19:46:09', 'admin', '2018-09-23 19:46:17', 0); INSERT INTO `sys_user` VALUES (8, 'xunyu', '荀彧', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 10, 'admin', '2022-08-17 19:46:38', 'admin', '2018-11-04 15:33:17', 0); INSERT INTO `sys_user` VALUES (9, 'sunquan', '孙权', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 10, 'admin', '2022-08-17 19:46:54', 'admin', '2018-09-23 19:47:03', 0); INSERT INTO `sys_user` VALUES (11, 'luxun', '陆逊', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 11, 'admin', '2022-08-17 19:47:44', 'admin', '2018-09-23 19:47:58', 0); INSERT INTO `sys_user` VALUES (12, 'huanggai', '黄盖', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 11, 'admin', '2022-08-17 19:48:38', 'admin', '2018-09-23 19:49:02', 0); INSERT INTO `sys_user` VALUES (34, 'zhouyu', '周瑜', NULL, 'fd80ebd493a655608dc893a9f897d845', 'YzcmCZNvbXocrsz9dm8e', 'test@qq.com', '13889700023', 1, 11, 'admin', '2022-08-17 19:47:28', 'admin', '2018-09-23 19:48:04', 0); SET FOREIGN_KEY_CHECKS = 1;
二、根据指定日期加365天,当前时间距离明年的指定的日期还有多少天-SQL编写
SELECT `name`, nick_name, TO_DAYS( ( create_time - INTERVAL 365 DAY ) ) - TO_DAYS( now( ) ) AS remainder_days FROM sys_user
SQL执行结果如下:
古今成大事者,不唯有超世之才,必有坚韧不拔之志!