【积累】mysql

mysql

  1. mysql查询某i个字段为空或不为空的sql
where 字段 is null 或者 where 字段 is not null

charwhere 字段=''
where 字段 <> ''

数字:
where isnull(字段)
  1. 将一个表中的数据插入到另一个表中的方法
    声明: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
  1. 检验某个数据库的某个表是否含有某个字段
-- 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;
  1. 查询某字段距离今天 近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
  1. 查询近六个月的数据
select * from 表名 where
DATE_SUB(CURDATE(),INTERVAL 6 month) <= 表名.字段名

-- 举例:
select * from student_table where
DATE_SUB(CURDATE(),INTERVAL 6 month) <= student_table.create_time
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值