MySQL常用查询语句

-- 删除索引
DROP INDEX index_url_object_id ON test2.mondaq;
ALTER TABLE table_name DROP INDEX index_name;

-- 添加唯一索引
ALTER TABLE test2.mondaq ADD UNIQUE (url_object_id);

-- 将多个表数据组合:前提是,表的字段要一直的
(select count(0) from osac  where create_date between '2019-06-01' and '2019-06-03')  union all
(select count(0) from grada where create_date between '2019-06-01' and '2019-06-03');

-- Mysql报错:1451 表关联生成的强制约束问题,在删除的时候回检查表之间的关联关系,从而导致无法删除。
SET foreign_key_checks = 0;  -- 先设置外键约束检查关闭
drop table table1;  -- 删除表,如果要删除视图,也是如此
SET foreign_key_checks = 1;   -- 开启外键约束检查,以保持表结构完整性

-- 修改字段的长度:change用来字段重命名,不能修改字段类型和约束; modify不用来字段重命名,只能修改字段类型和约束;
alter table table_name modify column name varchar(50) ;
alter table table_name change old_column new_column varchar(100) DEFAULT 1.2 COMMENT '注释'; -- 正常,此时字段名称没有改变,能修改字段类型、类型长度、默认值、注释

-- 插入数据
INSERT INTO `beta_quguoguo`.`qgg_resources` (`re_id`, `title`, `name`, `icon`, `href`, `spread`, `parentid`, `type`, `sort`, `menu`, `is_delete`, `create_date`, `update_date`) VALUES ('37', '桌码管理', 'tableCode', '', NULL, '0', '0', '1', '0', '0', '0', '2021-06-10 17:25:10', '2021-06-10 17:25:10');

--- 查看链接超时界限
show global variables like '%timeout';

-- 查看最大传递字节大小1M=1024*1024
show global VARIABLES like '%max_allowed_packet%';

-- 设置最大传递数据
set global max_allowed_packet = 1024*1024*1024;

-- 在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0:
alter table MyClass add passtest int(4) default '0';
alter table grada add crawl_time datetime DEFAULT NULL COMMENT '爬取数据时间';



-- 查询某一年数据
select id from cnn where year(create_date)=9999;

-- 删除某一年数据
DELETE FROM cnn WHERE id in (select id from cnn where year(create_date)=9999);

-- mysql版本问题,最前面加该语句
set sql_mode=(select replace(@@sql_mode,'NO_ZERO_IN_DATE,NO_ZERO_DATE',''));

-- 更新表里的数据
update table_name set website='cnn', name='cnn' where id=1;

查询

 A表          
 
  id   name  
 
  1  小王
 
  2  小李
 
  3  小刘
 
  B表
 
  id  A_id  job
 
  1  2    老师
 
  2  4    程序员

inner join (等值连接或者叫内连接):只返回两个表中连接字段相等的记录。

select a.name,b.job from A a  inner join B b on a.id=b.A_id
 
  只能得到一条记录
 
  小李  老师

left join (左连接):返回包括左表中的所有记录和右表中连接字段相等的记录。

select a.name,b.job from A a  left join B b on a.id=b.A_id
 
  三条记录
 
  小王  null
 
  小李  老师
 
  小刘  null

right join (右连接):返回包括右表中的所有记录和左表中连接字段相等的记录。

select a.name,b.job from A a  right join B b on a.id=b.A_id
 
  两条记录
 
  小李  老师
 
  null  程序员

full join (全外连接):返回左右表中所有的记录和左右表中连接字段相等的记录

select a.name,b.job from A a  full join B b on a.id=b.A_id
 
  四条数据
 
  小王  null
 
  小李  老师
 
  小刘  null
 
  null  程序员
Hole yor life get everything if you never give up.
原文地址:https://www.cnblogs.com/1fengchen1/p/15781973.html