07-查询操作(DQL)-多表查询

一. 综述

  查询操作主要从两个方面来说:单表查询和多表查询。 多表查询包括:笛卡尔积、外键约束、内连接查询、外链接查询、自连接查询。

二 . 案例设计

  1.  设计产品表(product)。包括:主键id、产品名称(productName)、分类编号(dir_id)、零售价(salePrice)、供应商(supplier)、品牌(brand)、折扣(cutoff)、成本价(costPrice)。

设计产品产品编号表( productdir)。 包括:主键id、编号名称( dirName)、父id( parent_id) 。

设计产品库存表( productstock)。包括:主键id、产品id( product_id )、库存数量( storeNum)、上次进库时间(lastIncomeDate)、上次出库时间( lastOutcomeDate)、预警数量(warningNum)。

对应的SQL语句:

 1 CREATE TABLE `product` (
 2   `id` bigint(11) NOT NULL AUTO_INCREMENT,
 3   `productName` varchar(50) DEFAULT NULL,
 4   `dir_id` bigint(11) DEFAULT NULL,
 5   `salePrice` double(10,2) DEFAULT NULL,
 6   `supplier` varchar(50) DEFAULT NULL,
 7   `brand` varchar(50) DEFAULT NULL,
 8   `cutoff` double(2,2) DEFAULT NULL,
 9   `costPrice` double(10,2) DEFAULT NULL,
10   PRIMARY KEY (`id`)
11 ) ENGINE=MyISAM AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;
12
13 -- ----------------------------
14 -- Records of product
15 -- ----------------------------
16 INSERT INTO `product` VALUES (‘1‘, ‘罗技M90‘, ‘3‘, ‘90.00‘, ‘罗技‘, ‘罗技‘, ‘0.50‘, ‘35.00‘);
17 INSERT INTO `product` VALUES (‘2‘, ‘罗技M100‘, ‘3‘, ‘49.00‘, ‘罗技‘, ‘罗技‘, ‘0.90‘, ‘33.00‘);
18 INSERT INTO `product` VALUES (‘3‘, ‘罗技M115‘, ‘3‘, ‘99.00‘, ‘罗技‘, ‘罗技‘, ‘0.60‘, ‘38.00‘);
19 INSERT INTO `product` VALUES (‘4‘, ‘罗技M125‘, ‘3‘, ‘80.00‘, ‘罗技‘, ‘罗技‘, ‘0.90‘, ‘39.00‘);
20 INSERT INTO `product` VALUES (‘5‘, ‘罗技木星轨迹球‘, ‘3‘, ‘182.00‘, ‘罗技‘, ‘罗技‘, ‘0.80‘, ‘80.00‘);
21 INSERT INTO `product` VALUES (‘6‘, ‘罗技火星轨迹球‘, ‘3‘, ‘349.00‘, ‘罗技‘, ‘罗技‘, ‘0.87‘, ‘290.00‘);
22 INSERT INTO `product` VALUES (‘7‘, ‘罗技G9X‘, ‘3‘, ‘680.00‘, ‘罗技‘, ‘罗技‘, ‘0.70‘, ‘470.00‘);
23 INSERT INTO `product` VALUES (‘8‘, ‘罗技M215‘, ‘2‘, ‘89.00‘, ‘罗技‘, ‘罗技‘, ‘0.79‘, ‘30.00‘);
24 INSERT INTO `product` VALUES (‘9‘, ‘罗技M305‘, ‘2‘, ‘119.00‘, ‘罗技‘, ‘罗技‘, ‘0.82‘, ‘48.00‘);
25 INSERT INTO `product` VALUES (‘10‘, ‘罗技M310‘, ‘2‘, ‘135.00‘, ‘罗技‘, ‘罗技‘, ‘0.92‘, ‘69.80‘);
26 INSERT INTO `product` VALUES (‘11‘, ‘罗技M505‘, ‘2‘, ‘148.00‘, ‘罗技‘, ‘罗技‘, ‘0.92‘, ‘72.00‘);
27 INSERT INTO `product` VALUES (‘12‘, ‘罗技M555‘, ‘2‘, ‘275.00‘, ‘罗技‘, ‘罗技‘, ‘0.88‘, ‘140.00‘);
28 INSERT INTO `product` VALUES (‘13‘, ‘罗技M905‘, ‘2‘, ‘458.00‘, ‘罗技‘, ‘罗技‘, ‘0.88‘, ‘270.00‘);
29 INSERT INTO `product` VALUES (‘14‘, ‘罗技MX1100‘, ‘2‘, ‘551.00‘, ‘罗技‘, ‘罗技‘, ‘0.76‘, ‘300.00‘);
30 INSERT INTO `product` VALUES (‘15‘, ‘罗技M950‘, ‘2‘, ‘678.00‘, ‘罗技‘, ‘罗技‘, ‘0.78‘, ‘320.00‘);
31 INSERT INTO `product` VALUES (‘16‘, ‘罗技MX Air‘, ‘2‘, ‘1299.00‘, ‘罗技‘, ‘罗技‘, ‘0.72‘, ‘400.00‘);
32 INSERT INTO `product` VALUES (‘17‘, ‘罗技G1‘, ‘4‘, ‘155.00‘, ‘罗技‘, ‘罗技‘, ‘0.80‘, ‘49.00‘);
33 INSERT INTO `product` VALUES (‘18‘, ‘罗技G3‘, ‘4‘, ‘229.00‘, ‘罗技‘, ‘罗技‘, ‘0.77‘, ‘96.00‘);
34 INSERT INTO `product` VALUES (‘19‘, ‘罗技G500‘, ‘4‘, ‘399.00‘, ‘罗技‘, ‘罗技‘, ‘0.88‘, ‘130.00‘);
35 INSERT INTO `product` VALUES (‘20‘, ‘罗技G700‘, ‘4‘, ‘699.00‘, ‘罗技‘, ‘罗技‘, ‘0.79‘, ‘278.00‘);
CREATE TABLE `productdir` (
  `id` bigint(11) NOT NULL auto_increment,
  `dirName` varchar(30) default NULL,
  `parent_id` bigint(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `productdir` VALUES (‘1‘, ‘鼠标‘, null);
INSERT INTO `productdir` VALUES (‘2‘, ‘无线鼠标‘, ‘1‘);
INSERT INTO `productdir` VALUES (‘3‘, ‘有线鼠标‘, ‘1‘);
INSERT INTO `productdir` VALUES (‘4‘, ‘游戏鼠标‘, ‘1‘);
CREATE TABLE `productstock` (
  `id` bigint(11) NOT NULL auto_increment,
  `product_id` bigint(11) default NULL,
  `storeNum` int(10) default NULL,
  `lastIncomeDate` datetime default NULL,
  `lastOutcomeDate` datetime default NULL,
  `warningNum` int(10) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `productstock` VALUES (‘1‘, ‘1‘, ‘182‘, ‘2015-03-12 20:33:00‘, ‘2015-03-12 20:33:04‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘2‘, ‘2‘, ‘27‘, ‘2015-03-02 20:33:28‘, ‘2015-03-09 20:33:40‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘3‘, ‘3‘, ‘89‘, ‘2015-02-28 20:34:13‘, ‘2015-03-12 20:34:19‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘4‘, ‘5‘, ‘19‘, ‘2015-03-01 20:34:43‘, ‘2015-03-12 20:34:48‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘5‘, ‘6‘, ‘3‘, ‘2015-02-01 20:35:12‘, ‘2015-03-02 20:35:16‘, ‘5‘);
INSERT INTO `productstock` VALUES (‘6‘, ‘7‘, ‘2‘, ‘2015-02-02 20:35:59‘, ‘2015-02-27 20:36:05‘, ‘3‘);
INSERT INTO `productstock` VALUES (‘7‘, ‘8‘, ‘120‘, ‘2015-03-12 20:36:31‘, ‘2015-03-12 20:36:33‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘8‘, ‘9‘, ‘58‘, ‘2015-03-02 20:36:50‘, ‘2015-03-12 20:36:53‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘9‘, ‘11‘, ‘28‘, ‘2015-03-02 20:37:12‘, ‘2015-03-12 20:37:15‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘10‘, ‘12‘, ‘8‘, ‘2015-03-02 20:37:35‘, ‘2015-03-09 20:37:38‘, ‘5‘);
INSERT INTO `productstock` VALUES (‘11‘, ‘13‘, ‘3‘, ‘2015-03-02 20:37:58‘, ‘2015-03-12 20:38:01‘, ‘5‘);
INSERT INTO `productstock` VALUES (‘12‘, ‘14‘, ‘6‘, ‘2015-03-02 20:38:20‘, ‘2015-03-07 20:38:23‘, ‘5‘);
INSERT INTO `productstock` VALUES (‘13‘, ‘15‘, ‘2‘, ‘2015-02-02 20:38:38‘, ‘2015-02-24 20:38:44‘, ‘5‘);
INSERT INTO `productstock` VALUES (‘14‘, ‘16‘, ‘3‘, ‘2015-02-02 20:39:05‘, ‘2015-02-06 20:39:09‘, ‘3‘);
INSERT INTO `productstock` VALUES (‘15‘, ‘17‘, ‘49‘, ‘2015-03-02 20:39:36‘, ‘2015-03-12 20:39:40‘, ‘20‘);
INSERT INTO `productstock` VALUES (‘16‘, ‘18‘, ‘14‘, ‘2015-03-02 20:39:57‘, ‘2015-03-09 20:40:01‘, ‘10‘);
INSERT INTO `productstock` VALUES (‘17‘, ‘20‘, ‘7‘, ‘2015-03-02 20:40:22‘, ‘2015-03-03 20:40:25‘, ‘5‘);

时间: 2024-07-30 10:19:14

07-查询操作(DQL)-多表查询的相关文章

(七)MySQL数据操作DQL:单表查询

(1)单表查询 1)环境准备 mysql> CREATE TABLE company.employee5( id int primary key AUTO_INCREMENT not null, name varchar(30) not null, sex enum('male','female') default 'male' not null, hire_date date not null, post varchar(50) not null, job_description varcha

(七)MySQL数据操作DQL:多表查询2

(1)准备环境 1)创建员工表 mysql> create table company.employee6( -> emp_id int auto_increment primary key not null, -> emp_name varchar(10), -> age int, -> dept_id int); mysql> insert into employee6(emp_name,age,dept_id) values -> ('tom',19,200

mysql第四篇:数据操作之多表查询

mysql第四篇:数据操作之多表查询 一.多表联合查询 #创建部门 CREATE TABLE IF NOT EXISTS dept ( did int not null auto_increment PRIMARY KEY, dname VARCHAR(50) not null COMMENT '部门名称' )ENGINE=INNODB DEFAULT charset utf8; #添加部门数据 INSERT INTO `dept` VALUES ('1', '教学部'); INSERT INT

mysql常用基础操作语法(四)--对数据的简单无条件查询及库和表查询【命令行模式】

1.mysql简单的查询:select 字段1,字段2...  from tablename; 如果字段那里写一个*,代表查询所有的字段,等同于指定出所有的字段名,因此如果要查询所有字段的数据,一般都是用*. 2.去重查询:select distinct 字段1,字段2... form tablename: 可以结合上一张图的结果来对比. 3.直接在查询时做数学四则运算,加减乘除余: 4.查询时给查询结果字段重命名:select 字段1 as 名称1,字段2 as 名称2 ... from ta

python 之 Django框架(orm单表查询、orm多表查询、聚合查询、分组查询、F查询、 Q查询、事务、Django ORM执行原生SQL)

12.329 orm单表查询 import os if __name__ == '__main__': # 指定当前py脚本需要加载的Django项目配置信息 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "orm_demo.settings") import django django.setup() # 启动Django项目 from app01 import models #返回QuerySet对象的方法: r

不使用left-join等多表关联查询,只用单表查询和Java程序,简便实现“多表查询”效果

上次我们提到,不使用left-loin关联查询,可能是为了提高效率或者配置缓存,也可以简化一下sql语句的编写.只写单表查询,sql真得太简单了.问题是,查询多个表的数据还是非常需要的. 因此,存在这么一个强烈的需求:查询2个集合,怎么合并2个集合中的数据为1个集合,且数据关联要正确.在实践中,我提炼了如下流程和工具方法: 流程  先查询第1个集合,根据第1个集合的结果,查询第2个集合,合并2个集合 public PageVo listPage(PageVo form) { List<Map<

Django ORM queryset object 解释(子查询和join连表查询的结果)

#下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) ---> <class 'django.db.models.query.QuerySet'> 查询出来是一个对象QuerySey 取值 print(books.values()) books = models.UserInfo.objects.filter()print(type(books))

mybatis调用mysql的存储过程(procedure),实现查询操作(student表中的某个年级中的总人数 select (1) 或者 select (*))

step1:在mysql cmd中新建存储过程: drop procedure if exists queryCountByGrade ; delimiter // -- 定义存储过程结束符号为// create procedure queryCountByGrade(IN gradenameinput INT(11),OUT counts int(11) begin select count(*) into counts from student where grade = gradename

SQL查询初学者指南读书笔记(五)集合操作与多表查询介绍

PART III:Thinking in Sets CHAPTER7 Thinking in Sets The three mostcommon set operations are as follows. Intersection Difference Union 在SQL中相应的关键词分别是 Intersection Except Union 实际数据库实现一般支持以下相应的数据库集合操作 INNER JOIN OUTER JOIN UNION JOIN 不过其区别是前者集合操作涉及表中所有