网站建设中端口号的作用是什么,中文在线っと好きだっ,微信审批小程序,做个外贸网站大概多少钱MySQL查询之多表查询#xff1a;#xff08;了解#xff09;但是还是要掌握比较好。 1、什么是多表查询#xff1f;
查询五子句是单表查询#xff0c;实际工作中#xff0c;也可能会接触到一个复杂的多表查询。 2、Union 联合查询#xff1a;
union联合查询的作用了解但是还是要掌握比较好。 1、什么是多表查询
查询五子句是单表查询实际工作中也可能会接触到一个复杂的多表查询。 2、Union 联合查询
union联合查询的作用是把多个表中的数据联合在一起进行显示。应用场景分库分表。
存储了1000万条数据查询比较慢我们会把表分割开创建两个到三个相同的数据表把数据平均分配到这些表中查询的时候将这几个表联合在一起。大型项目会涉及到分库分表。 第一步创建两个结构相同的学生表tb_student1和tb_student2;
操作演示
mysql create table tb_student1(- id mediumint not null auto_increment,- name varchar(20),- age tinyint unsigned default 0,- gender enum(男,女),- subject enum(ui,java,yunwei,python),- primary key(id)- ) engineinnodb default charsetutf8;
Query OK, 0 rows affected (0.00 sec)mysql
mysql create table tb_student2(- id mediumint not null auto_increment,- name varchar(20),- age tinyint unsigned default 0,- gender enum(男,女),- subject enum(ui,java,yunwei,python),- primary key(id)- ) engineinnodb default charsetutf8;
Query OK, 0 rows affected (0.00 sec)mysql
mysql
mysql insert into tb_student1 values (1,悟空,255,男,ui);
Query OK, 1 row affected (0.00 sec)mysql insert into tb_student2 values (2,唐僧,30,男,yunwei);
Query OK, 1 row affected (0.00 sec)mysql
mysql select * from tb_student1;
-----------------------------------
| id | name | age | gender | subject |
-----------------------------------
| 1 | 悟空 | 255 | 男 | ui |
-----------------------------------
1 row in set (0.00 sec)mysql select * from tb_student2;
-----------------------------------
| id | name | age | gender | subject |
-----------------------------------
| 2 | 唐僧 | 30 | 男 | yunwei |
-----------------------------------
1 row in set (0.00 sec)mysql
mysql select * from tb_student1 union select * from tb_student2;
-----------------------------------
| id | name | age | gender | subject |
-----------------------------------
| 1 | 悟空 | 255 | 男 | ui |
| 2 | 唐僧 | 30 | 男 | yunwei |
-----------------------------------
2 rows in set (0.00 sec)交叉查询了解
基本语法
mysql select */字段列表 from 数据表1,数据表2;
或
mysql select */字段列表 from 数据表1 cross join 数据表2;
运行结果
字段总数 数据表1的字段 数据表2的字段
总记录数 数据表1的记录数 * 数据表2的记录数 (笛卡尔积) 准备两张数据表产品分类表tb_category和产品信息表tb_goods。
mysql create table tb_category(- id smallint not null auto_increment,- name varchar(20),- pid smallint default 0,- primary key(id)- ) engineinnodb default charsetutf8;
Query OK, 0 rows affected (0.01 sec)mysql create table tb_goods(- id int not null auto_increment,- title varchar(80),- price decimal(11,2),- cid smallint default 0,- primary key(id)- ) engineinnodb default charsetutf8;
Query OK, 0 rows affected (0.02 sec)mysql insert into tb_category values (null,手机,0);
Query OK, 1 row affected (0.00 sec)mysql insert into tb_category values(null,电脑,0);
Query OK, 1 row affected (0.00 sec)mysql insert into tb_category values (null,游戏手机,1);
Query OK, 1 row affected (0.00 sec)mysql insert into tb_goods values (null,IPhone 11,5699.00,1);
Query OK, 1 row affected (0.00 sec)mysql insert into tb_goods values (null,MI 10,4699.00,1);
Query OK, 1 row affected (0.02 sec)mysql insert into tb_goods values (null,ThinkPad X1,9999.00,2);
Query OK, 1 row affected (0.01 sec)mysql insert into tb_goods values (null,Nike air,999.00,10);
Query OK, 1 row affected (0.00 sec)
说明
pid代表所属的父级类别如果自己就是顶级分类则为0
cid代表产品的所属分类编号与tb_category表要一一对应 案例把分类表和产品表进行交叉求结果
mysql select * from tb_category cross join tb_goods;
--------------------------------------------------------
| id | name | pid | id | title | price | cid |
--------------------------------------------------------
| 1 | 手机 | 0 | 1 | IPhone 11 | 5699.00 | 1 |
| 2 | 电脑 | 0 | 1 | IPhone 11 | 5699.00 | 1 |
| 3 | 游戏手机 | 1 | 1 | IPhone 11 | 5699.00 | 1 |
| 1 | 手机 | 0 | 2 | MI 10 | 4699.00 | 1 |
| 2 | 电脑 | 0 | 2 | MI 10 | 4699.00 | 1 |
| 3 | 游戏手机 | 1 | 2 | MI 10 | 4699.00 | 1 |
| 1 | 手机 | 0 | 3 | ThinkPad X1 | 9999.00 | 2 |
| 2 | 电脑 | 0 | 3 | ThinkPad X1 | 9999.00 | 2 |
| 3 | 游戏手机 | 1 | 3 | ThinkPad X1 | 9999.00 | 2 |
| 1 | 手机 | 0 | 4 | Nike air | 999.00 | 10 |
| 2 | 电脑 | 0 | 4 | Nike air | 999.00 | 10 |
| 3 | 游戏手机 | 1 | 4 | Nike air | 999.00 | 10 |
--------------------------------------------------------
12 rows in set (0.00 sec)交叉连接本身是没有意义的其只是强制把两个表甚至多个表进行连接在一起。但是交叉查询中也有正确的结果所以我们所谓的多表连接只需要在交叉连接的基础上增加一个连接条件则就是我们想要的结果了。所以交叉查询是多表查询的基础。 仔细查看交叉查询的结果可以看到有正确的结果也有错误的结果。我们只要把满足条件的正确的结果拿出来。