"""

1、字段修改

alter modify

alter change

alter add ''|first|after

alter drop

2、表关系:

一对一:外键存在两边都可以

一对多:外键存在多的一方

多对多:外键必须存在第三张关系表

外键:外键是表的一个字段,值可以重复也可以唯一,值是被关联表被关联字段的值,被关联字段必须有唯一键

foreign key(外键字段) references 被关联表(被关联字段)

create table book(

id int not null primary key auto_increment,

name varchar(64) unique

);

create table author(

id int not null primary key auto_increment,

name varchar(64) unique

);

create table book_author(

id int not null primary key auto_increment,

book_name varchar(64),

author_name varchar(64),

foreign key(book_name) references book(name)

on update cascade

on delete cascade,

foreign key(author_name) references author(name)

on update cascade

on delete cascade

);

"""

"""

1、单表查询

2、多表查询

3、子查询

"""

单表查询

"""

增:

insert [into]

[数据库名.]表名[(字段1[, ..., 字段n])]

values

(数据1[, ..., 数据n])[, ..., (数据1[, ..., 数据n])];

删:

delete from [数据库名.]表名 [条件];

改:

updata [数据库名.]表名 set 字段1=值1[, ..., 字段n=值n] [条件];

查:

select [distinct] 字段1 [[as] 别名1],...,字段n [[as] 别名n] from [数据库名.]表名 [条件];

"""

# 条件:from、where、group by、having、distinct、order by、limit => 层层筛选后的结果

# 注:一条查询语句,可以拥有多种筛选条件,条件的顺序必须按照上方顺序进行逐步筛选,distinct稍有特殊(书写位置),条件的种类可以不全

# 可以缺失,但不能乱序

去重:distinct

mysql>:

create table t1(

id int,

x int,

y int

);

mysql>: insert into t1 values(1, 1, 1), (2, 1, 2), (3, 2, 2), (4, 2, 2);

mysql>: select distinct * from t1; # 全部数据

mysql>: select distinct x, y from t1; # 结果 1,1 1,2 2,2

mysql>: select distinct y from t1; # 结果 1 2

# 总结:distinct对参与查询的所有字段,整体去重(所查的全部字段的值都相同,才认为是重复数据)

数据准备

CREATE TABLE `emp` (

`id` int(0) NOT NULL AUTO_INCREMENT,

`name` varchar(10) NOT NULL,

`gender` enum('男','女','未知') NULL DEFAULT '未知',

`age` int(0) NULL DEFAULT 0,

`salary` float NULL DEFAULT 0,

`area` varchar(20) NULL DEFAULT '中国',

`port` varchar(20) DEFAULT '未知',

`dep` varchar(20),

PRIMARY KEY (`id`)

);

INSERT INTO `emp` VALUES

(1, 'yangsir', '男', 42, 10.5, '上海', '浦东', '教职部'),

(2, 'engo', '男', 38, 9.4, '山东', '济南', '教学部'),

(3, 'jerry', '女', 30, 3.0, '江苏', '张家港', '教学部'),

(4, 'tank', '女', 28, 2.4, '广州', '广东', '教学部'),

(5, 'jiboy', '男', 28, 2.4, '江苏', '苏州', '教学部'),

(6, 'zero', '男', 18, 8.8, '中国', '黄浦', '咨询部'),

(7, 'owen', '男', 18, 8.8, '安徽', '宣城', '教学部'),

(8, 'jason', '男', 28, 9.8, '安徽', '巢湖', '教学部'),

(9, 'ying', '女', 36, 1.2, '安徽', '芜湖', '咨询部'),

(10, 'kevin', '男', 36, 5.8, '山东', '济南', '教学部'),

(11, 'monkey', '女', 28, 1.2, '山东', '青岛', '教职部'),

(12, 'san', '男', 30, 9.0, '上海', '浦东', '咨询部'),

(13, 'san1', '男', 30, 6.0, '上海', '浦东', '咨询部'),

(14, 'san2', '男', 30, 6.0, '上海', '浦西', '教学部'),

(15, 'ruakei', '女', 67, 2.501, '上海', '陆家嘴', '教学部');

常用函数

"""

拼接:concat() | concat_ws()

大小写:upper() | lower()

浮点型操作:ceil() | floor() | round()

整型:可以直接运算

"""

mysql>: select name,area,port from emp;

mysql>: select name as 姓名, concat(area,'-',port) 地址 from emp; # 上海-浦东

mysql>: select name as 姓名, concat_ws('-',area,port,dep) 信息 from emp; # 上海-浦东-教职部

mysql>: select upper(name) 姓名大写,lower(name) 姓名小写 from emp;

mysql>: select id,salary,ceil(salary)上薪资,floor(salary)下薪资,round(salary)入薪资 from emp;

mysql>: select name 姓名, age 旧年龄, age+1 新年龄 from emp;

条件:where

# 多条件协调操作导入:where 奇数 [group by 部门 having 平均薪资] order by [平均]薪资 limit 1

mysql>: select * from emp where id<5 limit 1; # 正常

mysql>: select * from emp limit 1 where id<5; # 异常,条件乱序

# 判断规则

"""

比较符合:> | < | >= | <= | = | !=

区间符合:between 开始 and 结束 | in(自定义容器)

逻辑符合:and | or | not

相似符合:like _|%

正则符合:regexp 正则语法

"""

mysql>: select * from emp where salary>5;

mysql>: select * from emp where id%2=0;

mysql>: select * from emp where salary between 6 and 9;

mysql>: select * from emp where id in(1, 3, 7, 20);

# _o 某o | __o 某某o | _o% 某o* (*是0~n个任意字符) | %o% *o*

mysql>: select * from emp where name like '%o%';

mysql>: select * from emp where name like '_o%';

mysql>: select * from emp where name like '___o%';

# sql只支持部分正则语法

mysql>: select * from emp where name regexp '.*\d'; # 不支持\d代表数字,认为\d就是普通字符串

mysql>: select * from emp where name regexp '.*[0-9]'; # 支持[]语法

分组与筛选:group by | having

where与having

# 表象:在没有分组的情况下,where与having结果相同

# 重点:having可以对 聚合结果 进行筛选

mysql>: select * from emp where salary > 5;

mysql>: select * from emp having salary > 5;

mysql>: select * from emp where id in (5, 10, 15, 20);

mysql>: select * from emp having id in (5, 10, 15, 20);

聚合函数

"""

max():最大值

min():最小值

avg():平均值

sum():和

count():记数

group_concat():组内字段拼接,用来查看组内其他字段

"""

分组查询 group by

# 修改my.ini配置重启mysql服务

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

# 在sql_mode没有 ONLY_FULL_GROUP_BY 限制下,可以执行,但结果没有意义

# 有 ONLY_FULL_GROUP_BY 限制,报错

mysql>: select * from emp group by dep;

# 分组后,表中数据考虑范围就不是 单条记录,因为每个分组都包含了多条记录,参照分组字段,对每个分组中的 多条记录 统一处理

# eg: 按部门分组,每个部门都有哪些人、最高的薪资、最低的薪资、平均薪资、组里一共有多少人

# 将多条数据统一处理,这种方式就叫 聚合

# 每个部门都有哪些人、最高的薪资、最低的薪资、平均薪资 都称之为 聚合结果 - 聚合函数操作的结果

# 注:参与分组的字段,也归于 聚合结果

mysql>:

select

dep 部门,

group_concat(name) 成员,

max(salary) 最高薪资,

min(salary) 最低薪资,

avg(salary) 平均薪资,

sum(salary) 总薪资,

count(gender) 人数

from emp group by dep;

mysql>: select

dep 部门,

max(age) 最高年龄

from emp group by dep;

# 总结:分组后,查询条件只能为 分组字段 和 聚合函数操作的聚合结果

分组后的having

mysql>:

select

dep 部门,

group_concat(name) 成员,

max(salary) 最高薪资,

min(salary) 最低薪资,

avg(salary) 平均薪资,

sum(salary) 总薪资,

count(gender) 人数

from emp group by dep;

# 最低薪资小于2

mysql>:

select

dep 部门,

group_concat(name) 成员,

max(salary) 最高薪资,

min(salary) 最低薪资,

avg(salary) 平均薪资,

sum(salary) 总薪资,

count(gender) 人数

from emp group by dep having min(salary)<2;

# having可以对 聚合结果 再进行筛选,where不可以

排序

排序规则

# order by 主排序字段 [asc|desc], 次排序字段1 [asc|desc], ...次排序字段n [asc|desc]

未分组状态下

mysql>: select * from emp;

# 按年龄升序

mysql>: select * from emp order by age asc;

# 按薪资降序

mysql>: select * from emp order by salary desc;

# 按薪资降序,如果相同,再按年龄降序

mysql>: select * from emp order by salary desc, age desc;

# 按龄降序,如果相同,再按薪资降序

mysql>: select * from emp order by age desc, salary desc;

分组状态下

mysql>:

select

dep 部门,

group_concat(name) 成员,

max(salary) 最高薪资,

min(salary) 最低薪资,

avg(salary) 平均薪资,

sum(salary) 总薪资,

count(gender) 人数

from emp group by dep;

# 最高薪资降序

mysql:

select

dep 部门,

group_concat(name) 成员,

max(salary) 最高薪资,

min(salary) 最低薪资,

avg(salary) 平均薪资,

sum(salary) 总薪资,

count(gender) 人数

from emp group by dep

order by 最高薪资 desc;

限制 limit

# 语法:limit 条数 | limit 偏移量,条数

mysql>: select name, salary from emp where salary<8 order by salary desc limit 1;

mysql>: select * from emp limit 5,3; # 先偏移5条满足条件的记录,再查询3条

连表查询

连接

# 连接:将有联系的多张表通过关联(有联系就行,不一定是外键)字段,进行连接,形参一张大表

# 连表查询:在大表的基础上进行查询,就称之为连表查询

# 将表与表建立连接的方式有四种:内连接、左连接、右连接、全连接

一对多数据准备

mysql>: create database db3;

mysql>: use db3;

mysql>:

create table dep(

id int primary key auto_increment,

name varchar(16),

work varchar(16)

);

create table emp(

id int primary key auto_increment,

name varchar(16),

salary float,

dep_id int

);

insert into dep values(1, '市场部', '销售'), (2, '教学部', '授课'), (3, '管理部', '开车');

insert into emp(name, salary, dep_id) values('egon', 3.0, 2),('yanghuhu', 2.0, 2),('sanjiang', 10.0, 1),('owen', 88888.0, 2),('liujie', 8.0, 1),('yingjie', 1.2, 0);

笛卡尔积

# 笛卡尔积: 集合 X{a, b} * Y{o, p, q} => Z{{a, o}, {a, p}, {a, q}, {b, o}, {b, p}, {b, q}}

mysql>: select * from emp, dep;

# 总结:是两张表 记录的所有排列组合,数据没有利用价值

内连接

# 关键字:inner join on

# 语法:from A表 inner join B表 on A表.关联字段=B表.关联字段

mysql>:

select

emp.id,emp.name,salary,dep.name,work

from emp inner join dep on emp.dep_id = dep.id

order by emp.id;

# 总结:只保留两个表有关联的数据

左连接

# 关键字:left join on

# 语法:from 左表 left join 右表 on 左表.关联字段=右表.关联字段

mysql>:

select

emp.id,emp.name,salary,dep.name,work

from emp left join dep on emp.dep_id = dep.id

order by emp.id;

# 总结:保留左表的全部数据,右表有对应数据直接连表显示,没有对应关系空填充

右连接

# 关键字:right join on

# 语法:from A表 right join B表 on A表.关联字段=B表关联字段

mysql>:

select

emp.id,emp.name,salary,dep.name,work

from emp right join dep on emp.dep_id = dep.id

order by emp.id;

# 总结:保留右表的全部数据,左表有对应数据直接连表显示,没有对应关系空填充

左右可以相互转化

mysql>:

select

emp.id,emp.name,salary,dep.name,work

from emp right join dep on emp.dep_id = dep.id

order by emp.id;

mysql>:

select

emp.id,emp.name,salary,dep.name,work

from dep left join emp on emp.dep_id = dep.id

order by emp.id;

# 总结:更换一下左右表的位置,相对应更换左右连接关键字,结果相同

全连接

mysql>:

select

emp.id,emp.name,salary,dep.name,work

from emp left join dep on emp.dep_id = dep.id

union

select

emp.id,emp.name,salary,dep.name,work

from emp right join dep on emp.dep_id = dep.id

order by id;

# 总结:左表右表数据都被保留,彼此有对应关系正常显示,彼此没有对应关系均空填充对方

一对一与一对多情况一致

# 创建一对一 作者与作者详情 表

create table author(

id int,

name varchar(64),

detail_id int

);

create table author_detail(

id int,

phone varchar(11)

);

# 填充数据

insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0);

insert into author_detail values(1, '13344556677'), (2, '14466779988'), (3, '12344332255');

# 内连

select author.id,name,phone from author join author_detail on author.detail_id = author_detail.id order by author.id;

# 全连

select author.id,name,phone from author left join author_detail on author.detail_id = author_detail.id

union

select author.id,name,phone from author right join author_detail on author.detail_id = author_detail.id

order by id;

多对多:两表两表建立连接

# 在一对一基础上,建立 作者与书 的多对多关系关系

# 利用之前的作者表

create table author(

id int,

name varchar(64),

detail_id int

);

insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0);

# 创建新的书表

create table book(

id int,

name varchar(64),

price decimal(5,2)

);

insert into book values(1, 'python', 3.66), (2, 'Linux', 2.66), (3, 'Go', 4.66);

# 创建 作者与书 的关系表

create table author_book(

id int,

author_id int,

book_id int

);

# 数据:author-book:1-1,2 2-2,3 3-1,3

insert into author_book values(1,1,1),(2,1,2),(3,2,2),(4,2,3),(5,3,1),(6,3,3);

# 将有关联的表一一建立连接,查询所以自己所需字段

select book.name, book.price, author.name, author_detail.phone from book

join author_book on book.id = author_book.book_id

join author on author_book.author_id = author.id

leftjoin author_detail on author.detail_id = author_detail.id;

数据库一对多做链接去重_数据库单表查询-多表查询相关推荐

  1. 数据库一对多做链接去重_数据库的查询命令

    条件 from / where / group by / having distinct / order by / limit 使用这些条件可以按照需求任意选择,但是顺序必须按照上面的顺序来 特殊点: ...

  2. mysql双主架构沈剑_58 沈剑 - 数据库架构师做什么-58同城数据库架构设计思路

    1.数据库架构师做什么? 58同城数据库架构设计思路 技术中心-沈剑 shenjian@58.com 2.关亍我-@58沈剑 • 前百度高级工程师 • 58同城技术委员会主席,高级架构师 • 58同城 ...

  3. mysql数据库链接百分号_数据库百分号怎么用

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  4. sql 数据库前两列值乘_数据库的基本概念:

    数据库的相关概念: 数据-data: 1.描述事物的符号 2.多种表现形式:文本,图形,音频,视频. 数据库-Database,DB 1.粮库,车库 2.存放数据的仓库在计算机中,按照一定的格式存放, ...

  5. pycharm连接mysql1193错误_pycharm连接mysql数据库提示错误的解决方法_数据库

    忘记phpmyadmin密码怎么重置_数据库 重置phpmyadmin密码的方法是:1.停止mysql服务:2.跳过验证启动mysql:3.重新设置密码:4.更新权限:5.重新启动mysql,如[ki ...

  6. mysql图书管理数据库的三个关系模式_数据库 考虑如下关于图书馆的关系模式,用关系代数写出查询(数据库系统概念第六版6.14)...

    贝尔梅尔娜美 2019.03.15 采纳率:60%    等级:39 已帮助:91565人 数据库系统的基本概念 数据:实际上就是描述事物的符号记录. 数据的特点:有一定的结构,有型与值之分,如整型. ...

  7. mysql数据库网上书店实训报告_数据库.网上书店实验报告.doc

    数据库.网上书店实验报告 数据库课程设计实验报告 网上书店 目 录 1.引言·························································· 2 1 ...

  8. 数据库查找姓李的人_数据库中查询姓李的老师的个数

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  9. mysql中以指定字段去重_数据库根据指定字段去重

    需求:对一张用户表根据name/email/card_num字段去除重复数据: 思路:用group by方法可以查询出'去重'后的数据,将这些数据存储到一张临时表中,然后将临时表的数据存储到指定的表中 ...

最新文章

  1. 佳能80d有人脸识别吗_国家地理2020年旅行者最推荐相机Top10,有你喜欢的吗?
  2. unity 在代码中创建spine动画组件
  3. sql获取一张表所有的字段_SQL语句19问
  4. SDH点对点接入与MPLS有什么区别?——Vecloud
  5. 【LeetCode从零单排】No38.CountAndSay
  6. 第二十六讲:基础一开放封闭原则
  7. bzoj4033 [HAOI2015]树上染色
  8. lua面向对象封装及元表(metatable)性能测试
  9. Educational Round 26 C. Two Seals
  10. UVa 10400 记忆化搜索
  11. 在Windows中使用FileZilla Server创建FTP
  12. 大学生性价比计算机推荐,2018大学生笔记本推荐_良心笔记本推荐【性价比之王】-太平洋电脑网...
  13. 读书笔记——实时渲染(一)
  14. 程序员工具箱(附各种工具的下载地址)
  15. Tragic Design 免积分下载
  16. vscode调用keil-MDK编译程序
  17. 演示Exchange用法
  18. 我想健康富有聪明怎么导告_富有成效的远程工作(当您的心理健康说“否”时)
  19. Jetpack Compose Modifier用法详解,如何保证高可用
  20. Mac使用系列之软件安装权限

热门文章

  1. ps查看完整程序执行路径
  2. ipad怎样和计算机连接网络,ipad怎么连接电脑教程 ipad怎么和电脑连接【详细步骤】...
  3. 程序人生 - 狗狗为什么会无缘无故地突然在家里飞奔呢?
  4. 微信小程序最新用户头像昵称获取规则调整应对措施(2022)
  5. Go语言基础知识01-用Go打个招呼
  6. HTML转TXT V1.0 简体中文绿色版
  7. 前端入门--CSS篇
  8. 三剑客grep sed awk
  9. 伯虎点秋香-rqnoj-144
  10. linux常用网络命令介绍