select auto_increment from information_schema.tables where table_schema='spd001'and table_name='b_storage_bin';
去重取ID
1 2 3 4
--去重取ID SELECT*FROM change_face_used_picture cfup INNERJOIN (SELECTMAX(id) as mid FROM change_face_used_picture WHERE user_id =15and `status` =2GROUPBY url ) t on cfup.id = t.mid LIMIT 5
update关联查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
-- 示例 UPDATE table_1 t1 left join table_2 t2 on t2.id = t1.tid SET t1.username = t2.uname where t1.id > 5; -- 先查询 SELECT t1.robot_code, LEFT(t1.model_code,15), t2.robot_code FROM `prompter-manager`.prompter_video_train_issue t1 LEFT JOIN (SELECT r.robot_code, s.scene_code FROM digital.scene s INNER JOIN digital.robot r on s.robot_id = r.id) t2 on LEFT( t1.model_code,15) = t2.scene_code WHERE t1.robot_code is null and t1.model_code != '' and t1.model_code != '233289519239237'; -- 后更新 update `prompter-manager`.prompter_video_train_issue t1 LEFT JOIN (SELECT r.robot_code, s.scene_code FROM digital.scene s INNER JOIN digital.robot r on s.robot_id = r.id) t2 on LEFT(t1.model_code,15) = t2.scene_code set t1.robot_code = t2.robot_code WHERE t1.robot_code is null and t1.model_code != '' and t1.model_code != '233289519239237';
SELECT*FROM s_user WHERE id>= ((SELECTMAX(id) FROM s_user)-(SELECTMIN(id) FROM s_user)) * RAND() + (SELECTMIN(id) FROM s_user) LIMIT 10
SELECT*FROM user_video_robot WHERE id>=(( SELECTMAX(id) FROM user_video_robot where create_time >'2024-03-25' )-( SELECTMIN(id) FROM user_video_robot where create_time >'2024-03-25' )) * RAND() + ( SELECTMIN(id) FROM user_video_robot where create_time >'2024-03-25' ) LIMIT 10
select*from user_video_robot uvr where id in(
select k.id from (
SELECTFLOOR( 7477+ RAND() * (7754-7477)) as id union SELECTFLOOR( 7477+ RAND() * (7754-7477)) as id
) k )
MySQL计算两日期/时间之间相差的秒数
1 2 3 4 5
SELECT TIMESTAMPDIFF(SECOND,'2020-03-27 10:38:00','2020-03-27 10:38:31') FROM DUAL;
-- 新增操作,当有重复键时忽略本次操作! insert ignore INTO `t_wx_group` (`id`, `wx_group_id`, `wx_group_name`) VALUES ( id, '2', '大盘4421群'); -- 更新操作:会刷新主键!!会先删除原有的数据记录,然后执行插入新的数据。 replace INTO `t_wx_group` (`id`, `wx_group_id`, `wx_group_name`) VALUES ( id, '2', '大盘4421群'); -- 新增操作,当遇到唯一索引的重键后执行更新操作 insert into`t_wx_group` (`wx_group_id`, `wx_group_name`) values ( '4', '222') on duplicate key update wx_group_name='222'; -- 新增操作,当遇到唯一索引的重键后执行更新操作 2 INSERT INTO student (name, age) SELECT * from (SELECT GROUP_CONCAT(name) as n, age FROM student_1 GROUP BY age) as t ON DUPLICATE KEY UPDATE name = t.n; -- 根据查询结果插入 INSERT INTO `user` (`user_id`, `corp_id`, `phone`, `user_name`, `user_sex`, `white_list`, `create_time`, `update_time`, `del_flag`) SELECT t1.id, t1.corp_id, t1.mobile,t1.username, 1,0,t1.create_time,IFNULL(t1.update_time,t1.create_time), 0 FROM scrm.sys_user t1 LEFT JOIN `user` t2 on t1.id = t2.user_id WHERE t1.del_flag = 0 and t2.user_id is null and t1.mobile is not null;
insert select where
1 2 3 4 5 6
-- 1 INSERTINTO t_dict ( name, code, parent_id, status ) SELECT'ZZZ', 'ZZZ', 0, 1FROM DUAL WHERENOTEXISTS (select id from t_dict where id =21560) -- 2 INSERTINTO tts_label_model ( model_id,label_id, order_no, create_time, update_time, is_deleted) select tm.id ,(select tlo.id from tts_label_order tlo where tlo.label_type ='df_category'and tm.user_category = tlo.label_name) as label_id , 0, now(), now(),0from tts_model tm where tm.id >=500331;
重新修改ID
UPDATE scene SET id = REPLACE(id, ‘0000’,’’) WHERE id > 95800336;
MICROSECOND SECOND MINUTE HOUR DAY WEEK MONTH QUARTER YEAR SECOND_MICROSECOND MINUTE_MICROSECOND MINUTE_SECOND HOUR_MICROSECOND HOUR_SECOND HOUR_MINUTE DAY_MICROSECOND DAY_SECOND DAY_MINUTE DAY_HOUR YEAR_MONTH
1 2 3 4 5 6 7 8 9 10 11
SELECT (case when (t1.delivery_time isnulland now() > DATE_ADD(t1.create_time,INTERVAL48HOUR)) then'已逾期' when (t1.delivery_time isnotnulland t1.delivery_time < t1.create_time) then'-' when (t1.delivery_time isnotnulland t1.delivery_time < DATE_ADD(t1.create_time,INTERVAL48HOUR)) then'未逾期' when (t1.delivery_time isnotnulland t1.delivery_time > DATE_ADD(t1.create_time,INTERVAL48HOUR)) then'逾期交付' else'制作中' end ) as overdueStatus FROM table_name_1 t1
CASE WHEN 操作
1 2 3 4 5 6 7 8 9 10 11
SELECT (case when (t1.delivery_time isnulland now() > DATE_ADD(t1.create_time,INTERVAL48HOUR)) then'已逾期' when (t1.delivery_time isnotnulland t1.delivery_time < t1.create_time) then'-' when (t1.delivery_time isnotnulland t1.delivery_time < DATE_ADD(t1.create_time,INTERVAL48HOUR)) then'未逾期' when (t1.delivery_time isnotnulland t1.delivery_time > DATE_ADD(t1.create_time,INTERVAL48HOUR)) then'逾期交付' else'制作中' end ) as overdueStatus FROM table_name_1 t1
GROUP_CONCAT
1 2 3 4 5 6 7 8 9
#### 在一行列出所有列: select GROUP_CONCAT(COLUMN_NAME SEPARATOR ', ') from INFORMATION_SCHEMA.Columns where table_name='tb_org'and table_schema='f6dms_20160522';
拼接到一行: SELECT GROUP_CONCAT(name SEPARATOR ', ') from city
#### 合并 mysql 一列多行 转换成一行 SELECT GROUP_CONCAT(tt.member_id SEPARATOR ', ') from t_member_info tt WHERE nickname isnotnull SELECT GROUP_CONCAT(tt.member_id SEPARATOR ','), '1'as name FROM t_member_info tt WHERE nickname isnotnullGROUPBY name
select'数字人克隆'as'类型', uvr.video_url as'原视频', concat('https://gy.cdn.guiji.cn', fw.synthesis_url) as'合成视频',fw.create_time as'创建时间' from `ffo-toc`.ffo_work fw leftjoin `ffo-toc`.user_video_robot uvr on write_param ->'$.robotId'= uvr.id where fw.create_time >'2024-03-11'and fw.create_time <'2024-03-18'and fw.synthesis_status =2and fw.task_type in(12)
) t orderby t.创建时间
select if(task_type =10,'视频翻译-有口型','视频翻译-无口型') as'类型' , -- write_param ->'$.videoUrl' as '原视频', -- JSON_EXTRACT(write_param, '$.videoUrl') as '原视频',
-- 去除双引号 write_param ->>'$.videoUrl'as'原视频', -- JSON_UNQUOTE(write_param ->'$.videoUrl') as '原视频', -- TRIM(BOTH '"' FROM JSON_EXTRACT(write_param, '$.videoUrl')) as '原视频', -- TRIM(BOTH '"' FROM write_param ->'$.videoUrl') as '原视频',
SELECT*from ( SELECT id as classify_id FROM `user_video_classify` WHERE name ='广告宣传'and type =2) t1 , ( SELECT id as bind_id FROM `user_video_tts` WHERE speaker ='阿若') t2;
查询重复数据、删除重复数据
1 2 3 4 5 6 7 8 9 10 11 12 13
-- 重复数据
SELECT age, count(age) FROM student GROUPBY age HAVINGCOUNT(age) >1;
-- 保留ID小的数据,删除age相同的数据 DELETE t1 FROM student t1 INNERJOIN student t2 WHERE t1.id > t2.id AND t1.age = t2.age;
-- 保留ID大的数据,删除age相同的数据 DELETE t1 FROM student t1 INNERJOIN student t2 WHERE t1.id < t2.id AND t1.age = t2.age;
查询统计
SELECT r.id, r.robot_name, IFNULL(b.status, 0) as ‘是否喜欢’, (select count() from user_video_behavior v where v.related_id = r.id and v.type = 1 and v.status = 1) as ‘likeCount’, (select count() from user_video_behavior v where v.related_id = r.id and v.type = 2 and v.status = 1) as ‘shareCount’ from user_video_robot r left join user_video_behavior b on r.id = b.related_id and b.type = 1 and b.user_id = 200 WHERE r.type = 2;
查询统计(有条件)
SELECT SUM(er.employ_duration) as ‘totalEmployDuration’, COUNT(*) as ‘totalEmployCount’, SUM(er.income) as ‘totalIncome’ , SUM(IF(er.status = 1,er.income,0)) as ‘canWithdrawalPrice’ , SUM(IF(er.status = 2,er.income,0)) as ‘auditWithdrawalPrice’ , SUM(IF(er.status = 3,er.income,0)) as ‘withdrawalPrice’ FROM user_video_robot r LEFT JOIN user_video_employ_record er on r.id = er.robot_id WHERE r.free_type = 5 and r.user_id = 100
MySQL中插入数据时使用条件NOT EXISTS
INSERT INTO user_video_behavior (related_id, type, user_id) SELECT 1, 1, 4 from dual WHERE not EXISTS ( SELECT 1 FROM user_video_behavior where related_id = 1 and type = 1 and user_id = 4 );
在 主键 或者 唯一约束 重复时,执行更新操作。
INSERT INTO user_video_behavior (id, related_id, type, user_id) values (1000, 1, 1, 4) ON DUPLICATE KEY UPDATE status = 2;
<selectid="queryTtsList"resultType="ai.guiji.face.dto.UserVideoTtsVo"> select * from (select t1.*, (case when (ifnull(t2.train_status, 20) = 20 and ifnull(t1.train_status, 20) = 20 and ifnull(t2.demo_video_make_status, 20) = 20 ) then '20' when (ifnull(t2.train_status, 20) = 30 or ifnull(t1.train_status, 20) = 30 or ifnull(t2.demo_video_make_status, 20) = 30 ) then '30' else '10' end ) as show_status from user_video_tts t1 left join user_video_robot t2 on t1.robot_id = t2.id ) ot <where> del_flag = 0 <iftest="id != null">and ot.id = #{id}</if> <iftest="userId != null">and ot.user_id = #{userId}</if> <iftest="type != null">and ot.type = #{type}</if> <iftest="lang != null and lang != ''">and (ot.lang is null or ot.lang = '' or ot.lang like concat('%',#{lang},'%'))</if> <iftest="lang != null and lang == ''">and (ot.lang is null or ot.lang = '')</if> <iftest="showStatusList != null and showStatusList.size > 0"> and ot.show_status in <foreachcollection="showStatusList"open="("close=")"separator=","item="status"> #{status} </foreach> </if> <iftest="speaker != null and speaker != ''">and ot.speaker = #{speaker}</if> </where> order by ot.create_time desc </select>