一 介绍 准备表 company.employeecompany.department 复制代码 #建表 create table department( id int, name varchar(20) ); create table employee( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id i
一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键字的执行优先级(重点) 重点中的重点:关键字的执行优先级 from #从库中找到某张表 where #用where约束条件从表中取出符合条件的数据 group by #将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组 having #将分组的结果进行having过
这是上一个sql更新某表字段的一个延伸,在更新表数据时,实际上会有多表数据查询场景,查询后,只需要更新某一个表中的数据,以下提供两个方法, 第一种使用update 两表查询 update api_manage_apicollectioninfo_copy a, api_manage_apicollectionmanage b set a.header=replace(a.header,'XXXDDD','zhangjun') WHERE a.api_collection_id=b.id and
转自https://blog.csdn.net/anxpp/article/details/73173274 update table1 t1 left join table2 t2 on t1.key=t2.key set t1.field1=t2.field1, t1.field2=t2.field2, t1.field3=t2.field3 where t1.field4 is null and t2.field4 > '2017-04-27';
考虑到开发人员有时候不小心误更新数据,要求线上库的 MySQL 实例都设置 sql_safe_updates=1 来避免没有索引的 update.delete. 结果有一天开发发现下面的一个SQL 没法正确执行: update t1 set col2=1 where key1 in (select col2 from t2 where key2='ABcD'); 错误如下: ERROR 1175 (HY000): You are using safe update mode and you tr
1. update table_A set table_A_column = ab.column from table_A aa left join table_B ab on aa.xx = ab.xx where aa.xx = xx 2. update table_A set table_A_column = table_B.column from table_A,table_B where table_A.xx = table_B.xx
一.多表查询 准备工作:创建两张表,部门表(department).员工表(employee),代码和表格如下: # 创建表 create table department( id int, name ) ); create table employee( id int primary key auto_increment, name ), gender enum('male','female') not null default 'male', age int, dep_id int ); #
/* 存在外键的表 删表限制: 1.先删除从表,再删除主表.(不能直接删除主表,主表被从表引用,尽管实际可能还没有记录引用) 建表限制: 1.必须先建主表,再建从表(没有主表,从表无法建立外键关系) */ DROP TABLE IF EXISTS employee; DROP TABLE IF EXISTS department; CREATE TABLE department( id INT PRIMARY KEY, name ) UNIQUE NOT NULL ); -- 建表时,添加外键
数据操作 插入数据(记录): 用insert: 补充:插入查询结果: insert into 表名(字段1,字段2,...字段n) select (字段1,字段2,...字段n) where ...; 更新数据update 语法: update 表名 set 字段1=值1,字段2=值2 where condition; 删除数据delete:delete from 表名 where condition; 查询数据select: 单表查询: 语法: select distinct 字段1,字段2.
单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二.关键字的执行优先级(重点) 重点中的重点:关键字的执行优先级 from where group by having select distinct order by limit 1.找到表:from 2.拿着where指定的约束条件,去文件/表中取出一条条记录 3.将取出的一条条记录进
约束 外键约束 外键约束概念 让表和表之间产生关系,从而保证数据的准确性! 建表时添加外键约束 为什么要有外键约束 -- 创建db2数据库 CREATE DATABASE db2; -- 使用db2数据库 USE db2; -- 创建user用户表 CREATE TABLE USER( id INT PRIMARY KEY AUTO_INCREMENT, -- id NAME VARCHAR(20) NOT NULL -- 姓名 ); -- 添加用户数据 INSERT INTO USER VAL