131 MySQLの単一テーブルのクエリ(重要)

まず、構文およびキーワードのシーケンスは、単一テーブルのクエリを実行します

1.1単一テーブルのクエリ構文

SELECT DISTINCT 字段1,字段2... FROM 表名
                              WHERE 条件
                              GROUP BY field
                              HAVING 筛选
                              ORDER BY field
                              LIMIT 限制条数

1.2実行の重要な優先順位

  1. From:テーブルを検索します
  2. ここで指定された制約は保持している場合は、ファイルに/テーブルが取ら記録
  3. グループ:いいえグループ、グループとして全体が存在しない場合は、1つは、によって抽出されたグループのグループを記録します
  4. 有する:グルーピングの結果をHAVING濾過し
  5. 明確な:重複排除
  6. ORDER BY:条件で並べ替え結果:ORDER BY
  7. 制限:表示される結果の数を制限します

注意:クエリは、あなたがスクリーニング条件の多様性を持つことができ、順序の条件は、上記の順序で段階的に選択を行う必要があり、明確な少し特殊な条件のようなものがなく、注文のうち、削除することができ、(位置を書きます)

第二に、単純なクエリ

"""
增:
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 => 层层筛选后的结果

2.1データ準備

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, '上海', '陆家嘴', '教学部');

第三に、デエンファシス(異なります)

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

要約:異なる問い合わせのすべてのフィールドに参加し、全体的なデエンファシス(同じ調査のすべてのフィールドの値は、重複データであると考えられていました)

第四に、一般的に使用される機能

  • ステッチ:CONCAT() | CONCAT_WS()ステッチが第一の位置に文字をCONCAT_WSことを除いては、バックステッチをフィールドに置きます。
  • ケース:アッパー() | LOWER()
  • 浮動小数点演算:CEIL()切り上げは| フロア() |切り捨てラウンド()の丸めを
  • 整数:直接操作
  • 別名:限り、フィールドクエリのエイリアスを使用することができるよう、キーワード、それは省略することができます
mysql>: select name,area,port from emp;
# 拼接:concat() | concat_ws()
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;

V.制約()

判定ルール:

  1. 比較一致:>| <| >=| <=| =|!=
  2. インターバル試合:開始と終了BETWEEN |中(カスタムコンテナ)
  3. ロジックマッチ:and| or|not
  4. ファジーマッチ:like'N-%'
    • ワイルドカードは%や_することができ、
      • %任意の数の文字を表します
      • _ 1つの文字を表します
  5. 定期的な試合:正規表現正規文法
# 多条件协调操作导入:where 奇数 [group by 部门 having 平均薪资] order by [平均]薪资 limit 1

# 比较匹配
mysql>: select * from emp where salary>5;   # 查询salary>5的全部记录
mysql>: select * from emp where id%2=0;     # 查询id为偶数的全部记录
mysql>: select * from emp where id%2=1;     # 查询id为奇数的全部记录

# 区间匹配
mysql>: select * from emp where salary between 6 and 9; # 查询 salary 在 6-9 范围内的记录

# 模糊匹配
mysql>: select * from emp where name like "%w%";    # 匹配name字段 中包含w的记录
mysql>: select * from emp where name like "_w%";    # 匹配name字段 中包含一个任意字符w的记录
mysql>: select * from emp where name like "__e%";   # 匹配name字段 中包含两个任意字符w的记录

# sql只支持部分正则语法
mysql>: select * from emp where name regexp '.*[0-9]';  # 支持[]语法

六、集約関数

  • MAX():最大値
  • 分():最小値
  • AVG():平均
  • 合計():サム
  • 数():
  • GROUP_CONCAT():グループフィールドスプライシング

彼は強調した:重合性機能基含有量を重合していないパケットの場合、デフォルトのセットです。

# 求全部记录的个数
mysql>: select count(*) from emp;

# 求字段salary的最大数据
mysql>: select max(salary) from emp;

# 求字段salary的最小数据
mysql>: select min(salary) from emp;

# 求字段salary的平均数据
mysql>: select avg(salary) from emp;

# 对 字段salary的数据 求和
mysql>: select sum(salary) from emp;

七、グループ化とフィルタリング(によってグループ|有するもの)

7.1パケットクエリ(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(salary) 部门人数 
from emp group by dep;

# eg: 按部门分组,查询每个组年龄最大的记录
mysql>: select dep 部门, max(age) 最高年龄 from emp group by dep;

7.2ここ有するの差

  • パケットが存在しないこと、を有する同じ結果に
  • キー:重合は結果をフィルタリングすることができました
# 没有分组进行筛选,没有区别
mysql>: select * from emp where salary > 5;
mysql>: select * from emp having salary > 5;

7.3パケットをした後

  • キー:重合は結果をフィルタリングすることができました
# eg: 按部门分组,查询每个部门中最低薪资小于5的记录
mysql>: 
select
    dep 部门,
    group_concat(name) 成员,
    max(salary) 最高薪资,
    min(salary) 最低薪资,
    avg(salary) 平均薪资,
    count(salary) 部门人数
from emp group by dep having min(salary) < 5;

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

八、ソート(順)

  • 昇順(低から高へ): asc(デフォルトの昇順)
  • (最低から最高)降順:desc

8.1照合順序

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

8.2グループ化されていません

mysql>: select * from emp

# 按年龄升序(从低到高)
mysql>: select * from emp order by age asc;
# 按年龄降序(从高到低)
mysql>: select * from emp order by age desc;

8.3ステータスパケット

# 按部门分组后, 排序部门人数为降序(从高到低)
mysql>: 
select 
    dep 部门,
    group_concat(name) 成员,
    max(salary) 最高薪资,
    min(salary) 最低薪资,
    avg(salary) 平均薪资,
    sum(salary) 总薪资,
    count(salary) 部门人数
from emp group by dep order by 部门人数 desc;

# 按部门分组后, 排序部门人数为升序(从低到高)
mysql>: 
select 
    dep 部门,
    group_concat(name) 成员,
    max(salary) 最高薪资,
    min(salary) 最低薪资,
    avg(salary) 平均薪资,
    sum(salary) 总薪资,
    count(salary) 部门人数
from emp group by dep order by 部门人数;    # 默认升序

九、制限の上限

構文:数制限|リミットオフセット、バーの数を

# 语法: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条

テン、でもテーブルクエリ

  • 接続:(必ずしも接続線、外部キー)リンクによって連結された複数のテーブルが存在することになるフィールド、接続、大テーブルパラメータ
  • でも、テーブル、クエリ:大きなテーブルに基づいてクエリが、それは偶数テーブルクエリと呼ばれています
  • テーブルとの接続を確立するのテーブルは、4つある:内部接続は、左右の接続部を接続し、完全に接続

データ準備

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(inner可以省略)
  • 構文:from A表 inner join B表 on A表.关联字段=B表.关联字段
mysql>: 
select emp.id,emp.name,salary,dep.name,work 
from dep inner join emp on dep.id=emp.dep_id;   # 内连接的inner可以省略

概要:唯一の2つのテーブルを保持してデータを関連付けられています

左の接続

  • キーワード:left join on

  • 構文:from A表 left join B表 on A表.关联字段=B表.关联字段

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表 left 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;

概要:テーブルの周りにどのような位置を置き換え、対応する左右の接続キーを置き換え、同じ結果

完全に接続されています

キーワードで接続されている完全な接続を達成するために、左右の接続

キーワード:union

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', 2);
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 from book 
join author_book on book.id = author_book.book_id
join author on author_book.author_id = author.id;

# 多对多对1
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
left join author_detail on author.detail_id = author_detail.id;

おすすめ

転載: www.cnblogs.com/XuChengNotes/p/11588528.html