MySQL与Mybatis相关记录

334 阅读1分钟

1.mysql 查询数据库时时间戳的转换

1、将时间转换为时间戳,返回无符号整数单位为秒,如果参数为空,则处理为当前时间。 unix_timestamp() unix_timestamp(date):date 可以是一个DATE 字符串、一个 DATETIME字符串、一个 TIMESTAMP或一个当地时间的YYMMDD 或YYYMMDD格式的数字。 例如: select unix_timestamp() select unix_timestamp('2021-08-26 10:40:53')

2、将时间戳转换为时间 from_unixtime(unix_timestamp) from_unixtime(unix_timestamp,format) 返回'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS 格式值的unix_timestamp参数表示,具体格式取决于该函数是否用在字符串中或是数字语境中。 若format 已经给出,则结果的格式是根据format 字符串而定。 例如: select from_unixtime(1629945653) select from_unixtime(1629945653,'%Y-%m-%d')

2.判断要创建的表是否已经存在

select count(*) from information_schema.TABLES where table_schema = 'db' # db:数据库名称 and table_name = 'table' # table:表名称

3、建表和删除表在mybatis中的写法

<update id="createNewTable" parameterType="String"> CREATE TABLE ${table} ( id bigint(20) NOT NULL AUTO_INCREMENT, mobile varchar(20) NOT NULL, rate int(11) NOT NULL, PRIMARY KEY (id)) </update>

/*删除表,table:表名 */ <update id="dropTable"> drop table if exists ${table} </update>

4、查询某个字段是否存在某个数值 select * from tree where FIND_IN_SET(666,字段名) select * from tree where locate(666,字段名) select * from tree where 字段名 like '%666%'

5、count函数加条件总结

使用count()函数实现条件统计的基础是对于值为NULL的记录不计数,常用的有以下三种方式,假设统计num大于200的记录 select count(num > 200 or null) from test; select count(if(num > 200, 1, null)) from test select count(case when num > 200 then 1 end) test a

6、case when使用

update ymcs_user_banlance setbalance= caseopenIdwhen '1111111' then 222222 when '3333333' then 444444 end whereopenId in (1111111,3333333)