/*
2   需求:建立产品和分类表
1.查询每种分类的产品数量,没有产品的分类也要统计。(cname,quantity)
2.根据分类名称查询分类中的所有产品
*/ -- -------------------
-- category
-- 先建立主表,再建立从表,可以在从表创建时添加外键。
-- -------------------
drop table if exists category;
create table category(
cid int unsigned key auto_increment,
cname varchar(255)
);
-- show create table category; -- -------------------
-- product
-- -------------------
drop table if exists product;
create table product(
pid int unsigned key auto_increment,
pname varchar(255),
price decimal(10, 2),
cid int unsigned,
constraint category_fk foreign key (cid) references category(cid)
);
-- show create table product; -- -------------------
-- 插入测试数据
-- -------------------
insert into category(cname) values('蔬菜');
insert into category(cname) values('水果');
insert into category(cname) values('饮料'); insert into product (pname, price, cid)
values('豆角', 2.35, (select cid from category where cname='蔬菜'));
insert into product (pname, price, cid)
values('萝卜', 1.5, (select cid from category where cname='蔬菜'));
insert into product (pname, price, cid)
values('香蕉', 3.6, (select cid from category where cname='水果'));
insert into product (pname, price, cid)
values('苹果', 3.6, null); -- -------------------
-- 1.查询每种分类的产品数量,没有产品的分类也要统计。(cname,quantity)
-- -------------------
select cname, count(*) quantity
from product p right join category c
on p.cid=c.cid
group by cname; -- -------------------
-- 2.根据分类名称查询分类中的所有产品
-- -------------------
-- 方法1 内连接
select p.pname, p.price
from product p join category c
on p.cid=c.cid and c.cname='蔬菜'; -- 方法2 子查询
select p.pname, p.price
from product p
where p.cid=(select c.cid from category c where cname='蔬菜'); -- -------------------
-- 3.使用union实现全外连接
-- -------------------
select * from product p left join category c
on p.cid=c.cid
union
select * from product p right join category c
on p.cid=c.cid;

mysql8必知必会7 连接 内连接 外连接 交叉连接的更多相关文章

  1. 【SQL必知必会笔记(1)】数据库基础、SQL、MySQL8.0.16下数据库、表的创建及数据插入

    文章目录 1.数据库基础 1.1 数据库(database) 1.2 表(table) 1.3 列和数据类型 1.4 行 1.5 主键 2.什么是SQL 3.创建后续练习所需数据库.表(MySQL8. ...

  2. 《SQL必知必会》学习笔记二)

    <SQL必知必会>学习笔记(二) 咱们接着上一篇的内容继续.这一篇主要回顾子查询,联合查询,复制表这三类内容. 上一部分基本上都是简单的Select查询,即从单个数据库表中检索数据的单条语 ...

  3. mysql必知必会

    春节放假没事,找了本电子书mysql必知必会敲了下.用的工具是有道笔记的markdown文档类型. 下面是根据大纲已经敲完的章节,可复制到有道笔记的查看,更美观. # 第一章 了解SQL## 什么是S ...

  4. 关于TCP/IP,必知必会的十个经典问题[转]

    关于TCP/IP,必知必会的十个问题 原创 2018-01-25 Ruheng 技术特工队   本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养. 一 ...

  5. Android程序员必知必会的网络通信传输层协议——UDP和TCP

    1.点评 互联网发展至今已经高度发达,而对于互联网应用(尤其即时通讯技术这一块)的开发者来说,网络编程是基础中的基础,只有更好地理解相关基础知识,对于应用层的开发才能做到游刃有余. 对于Android ...

  6. 《MySQL必知必会》整理

    目录 第1章 了解数据库 1.1 数据库基础 1.1.1 什么是数据库 1.1.2 表 1.1.3 列和数据类型 1.1.4 行 1.1.5 主键 1.2 什么是SQL 第2章 MySQL简介 2.1 ...

  7. 迈向高阶:优秀Android程序员必知必会的网络基础

    1.前言 网络通信一直是Android项目里比较重要的一个模块,Android开源项目上出现过很多优秀的网络框架,从一开始只是一些对HttpClient和HttpUrlConnection简易封装使用 ...

  8. SQL 必知必会 总结(一)

    SQL必知必会 总结(一) 第 1 课 了解SQL 1.数据库(database): 保存有组织的数据容器(通常是一个文件或一组文件). 2.数据库管理系统(DBMS): 数据库软件,数据库是通过 D ...

  9. TCP/IP 必知必会的十个问题

    本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养. 一.TCP/IP模型 TCP/IP协议模型(Transmission Control Protoc ...

  10. H5系列之地理位置(必知必会)

    H5之地理位置必知必会     [02]概念   规范地址:http://www.w3.org/TR/geolocation-API/     HTML5 Geolocation(地理定位)用于定位用 ...

随机推荐

  1. javascript Date对象的介绍及linux时间戳如何在javascript中转化成标准时间格式

    1.Date对象介绍 Date对象具有多种构造函数.new Date()new Date(milliseconds)new Date(datestring)new Date(year, month)n ...

  2. leetcode 792. Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  3. dhcpcd守护进程分析【转】

    本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073 最近在调android ethernet功能,android本身不带 e ...

  4. SCAU 1138 代码等式 并查集

    1138 代码等式[附加题] 该题有题解 时间限制:500MS  内存限制:65536K 提交次数:59 通过次数:21 题型: 编程题   语言: G++;GCC Description 一个代码等 ...

  5. True(False) Positives (Negatives), 召回率和精度定义

    True Positive (真正, TP)被模型预测为正的正样本: True Negative(真负 , TN)被模型预测为负的负样本 : False Positive (假正, FP)被模型预测为 ...

  6. 用php描述二分查找法

    //二分查找 $arr = array(0,1,2,3,4,5,6,7,8,9); function bin_sch($array, $low, $high, $k){ if ($low <= ...

  7. hdu-5726 GCD(rmq)

    题目链接: GCD Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Prob ...

  8. H3C-路由器密码恢复

    路由器密码恢复: 1.先关闭电源,重新启动路由器,注意终端上显示 press CTRL+B to enter extended boot menu 的时候,我们迅速按下ctrl+B,这样将进入扩展启动 ...

  9. 【转载】rageagainstthecage.c源码以及注释

    如下: //头文件包含 #include <stdio.h> #include <sys/types.h> #include <sys/time.h> #inclu ...

  10. bootstrap table 根据单元格中的数据改变单元格的样式

    在bootstrap-table.js里面列属性 formatter就是用来格式化单元格的,其默认值是undefined 类型是function,function(value,  row, index ...