【积累】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

相关推荐

  1. 积累mysql

    2024-04-05 03:54:02       42 阅读
  2. 积累】list

    2024-04-05 03:54:02       47 阅读
  3. Bug积累

    2024-04-05 03:54:02       38 阅读
  4. linux命令积累

    2024-04-05 03:54:02       55 阅读
  5. 知识点积累

    2024-04-05 03:54:02       55 阅读
  6. uniapp技术积累

    2024-04-05 03:54:02       49 阅读
  7. Layui技术积累

    2024-04-05 03:54:02       53 阅读
  8. 向量数据库知识积累

    2024-04-05 03:54:02       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 03:54:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 03:54:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 03:54:02       82 阅读
  4. Python语言-面向对象

    2024-04-05 03:54:02       91 阅读

热门阅读

  1. mysql常见故障

    2024-04-05 03:54:02       37 阅读
  2. 4.2总结

    4.2总结

    2024-04-05 03:54:02      41 阅读
  3. 【leetcode面试经典150题】10.跳跃游戏 II(C++)

    2024-04-05 03:54:02       42 阅读
  4. 搭建本地YUM仓库

    2024-04-05 03:54:02       42 阅读
  5. C# OpenFileDialog

    2024-04-05 03:54:02       39 阅读
  6. 时间复杂度和空间复杂度

    2024-04-05 03:54:02       37 阅读
  7. Linux系统NVME SSD上下电流程梳理

    2024-04-05 03:54:02       37 阅读
  8. 如何成为一名独立开发者

    2024-04-05 03:54:02       44 阅读
  9. rust 自定义安装 error: linker `link.exe` not found

    2024-04-05 03:54:02       38 阅读
  10. 两种C链表接口构造方式

    2024-04-05 03:54:02       38 阅读
  11. 五、c++代码中的安全风险-memcpy

    2024-04-05 03:54:02       35 阅读