关键字
1...mysql包含之in
SELECT count(1) as count FROM 表名 a,表名 b WHERE a.主键 = b.外键 and a.字段 in('1','2,'3') and a.字段<> 1;
2.and与or的结合使用
update 表名SET 字段1= 99,s字段2 = 1 WHERE (字段3= '1' and 字段4 <> '99') OR (s字段5<>1 AND 字段6 = 99) ;
3.instr与in
update sr表名 set sys_spzt = 0 where mhzsfz in (342623199709098090 , 3301026410244561245 , 33010252 , 3301120252);
update sr_main_ww set sys_spzt = 0 where INSTR('33011203022252,3301120252', mhzsfz) >= 1
select count(1) as ncount from dsr_Hc_jz where JSFCB = 1 and (INSTR(jycbm,'dsr_hc_rs')>0 or INSTR(jycbm,'dsr_hc_gjj')>0) and sys_zdrq like concat(?,'%')
SELECT * FROM 表名 WHERE 字段1= 1 AND 字段2 = 0 and (字段3= '' or 字段4 is null or 字段5 = '' or 字段6 is null or 字段7= '' or 字段8 is null) and instr('db_jz,kn_jz,wb_jz',字段9) > 0;
4.not in的使用
select 学生学号 from 表名 where 学生学号 not in(SELECT 学生学号 FROM 表名2 A,表名3 B WHERE A.主键= B.外键 AND A.学生学号= B.学生学号);
5.CONCAT 拼接字符串
-- 日期函数
select CURRENT_DATE,CURRENT_TIME,CURRENT_TIMESTAMP,year(CURRENT_DATE),month(CURRENT_DATE),SYSDATE();
select * from sr_main where TIMESTAMPDIFF(DAY,SYS_CREATETIME,SYSDATE())>7 and SYS_SPZT <>1 ;
select TIMESTAMPDIFF(DAY,'2020-02-9',SYSDATE());
select TIMESTAMPDIFF(DAY,'2020-02-9 17:09:39',SYSDATE());
其他函数:
1.ROUND,保留2位小数第二个参数则写2
select ROUND(sum(mzjzje)/10000,2) as bmje207 from shjz_sjzhk_da a where sjbfnf = ? and xzqh like '33%' and INSTR('ywgl_sylbzj',a.mdjlx)>0 and SYS_SCBJ =0
2.sum、ifnull、TIMESTAMPDIFF的使用(比如接口的平均调用的返回时间计算)
SELECT
count,
(zsj / count) AS pjdysj
FROM
(
SELECT
count(1) AS count,
ifnull(
sum(
TIMESTAMPDIFF(DAY, a.DTJRQ, b.RKSJ)
),
0
) AS zsj
FROM
dsr_hc_bm a,
dsr_hc_hy b
WHERE
a.dbmbh = 'dsr_hc_hy'
AND a.DTJRQ LIKE CONCAT('2019', '%')
AND b.RKSJ LIKE CONCAT('2019', '%')
AND a.dhcid = b.hyhcid
) c
3.DATE_FORMAT的使用
//根据身份证年龄计算
SELECT
count(1) AS count
FROM
bd_user
WHERE
DATE_FORMAT(NOW(), '%Y') - SUBSTRING(usfz, 7, 4) >=0
AND DATE_FORMAT(NOW(), '%Y') - SUBSTRING(usfz, 7, 4) <=18
AND UTYPE = 999