Mysql 工作常用函数
生成时间戳:
unix_timestamp(now())
for循环:
<foreach collection="id" index="index" item="item" open="(" separator="," close=")" > #{item}</foreach>
查重
HAVING COUNT(*) > 1
GROUP BY ORDER BY LIMIT 0,3
GROUP BY 分组;
ORDER BY 排序;
LIMIT 分页;
ASE 正序;
DESC 倒序;
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
GROUP_CONCAT(s.id) AS schoolIds
to_days()用法查询今天的数据
TO_DAYS(create_time) = TO_DAYS(NOW());
find_in_set:
第二个参数需为String
find_in_set(a.id, #{ids})
isnull函数
select * from student where isnull(gender)ifnull函数select id, ifnull(gender, '未知') as gender from student
NULLIF是另一个二元函数,主要用来看二个表字段的数据是否相同的。当二个字段数据相同,该函数就会返回null,如果不相同,则会返回第一个参数的值。
select id, nullif(id, gender) as gender from student
查询表名
select table_name from information_schema.tables where table_schema='库名' and table_name = "表名" ;
复制表
CREATE TABLE school_course_study_list_12 LIKE school_course_study_list_
创建唯一索引
create unique index UK_student_name on student (name);
alter table student add constraint uk_student_name unique (name);
student表名 uk_student_name 索引名称 name表字段
创建复合索引
ALTER TABLE u_user
ADD CONSTRAINT uc_PersonID UNIQUE (student_no,school_id)
u_user表名 uc_PersonID 索引名称 student_no,school_id 表多个字段