mysql
- mysql查询某i个字段为空或不为空的sql
where 字段 is null 或者 where 字段 is not null
char:
where 字段=''
where 字段 <> ''
数字:
where isnull(字段)
- 将一个表中的数据插入到另一个表中的方法
声明:a,b都是表
如果两个表字段相同:
-- b表存在(两表结构一样)
insert into b select * from a
若两表只是有部分(字段)相同,则
-- col指代字段名
insert into b(col1,col2,col3,col4...) select col1,col2,col3,col4,... from a where ...
把表a插入到表b中去
-- b表不存在
select * into b from a
select (字段1,字段2...) into b from a
表字段不同
-- 将表b中的某些字段插入到表a中, 表a和b结构不同
-- 注意:表字段名称必须相同,如果从表b中查出来的字段名不同可以使用as重命名 重命名为和表a对应字段相同的名字
insert into 表a(字段1,字段2,字段3,...)
select 字段1,字段2,字段3,... form 表b
- 检验某个数据库的某个表是否含有某个字段
-- table_schema 数据库名
-- table_name 表名
-- column_name 字段名
select count(*) from information_schema.columns where table_schema = '数据库名' and table_name = '表名' and column_name = '字段名';
-- 返回的是0 没有这个字段,返回的是1有这个字段。
-- 返回字段名的:
select column_name from information_schema.columns where table_schema = '数据库名' and table_name = '表名' and column_name = '字段名';
-- 如果字段存在返回字段名,不存在返回空
-- 如果字段不存在,添加字段
alter table 表名
add column if not exists 字段名 字段数据类型;
-- 删除表中的某个字段,并且在执行前先判断该字段是否存在
if exists (select * from information_schema.columns where table_name '表名' and column_name = '字段名') then
alter table 表名 drop column 字段名;
end if;
- 查询某字段距离今天 近30天的数据
例如查询以创建时间计算 过去三十天的数据
-- 字段名是创建时间
select * from 表名 where
DATE_SUB(CURDATE(),INTERVAL 30 day) <= 表名.字段名
-- 举例:
select * from student_table where
DATE_SUB(CURDATE(),INTERVAL 30 day) <= student_table.create_time
- 查询近六个月的数据
select * from 表名 where
DATE_SUB(CURDATE(),INTERVAL 6 month) <= 表名.字段名
-- 举例:
select * from student_table where
DATE_SUB(CURDATE(),INTERVAL 6 month) <= student_table.create_time