SQL内 连接(只连接匹配的行)

/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2012/8/19 8:45:30                            */
/*==============================================================*/


drop table if exists store_information;

drop table if exists geography;


/*==============================================================*/
/* Table: store_information                                     */
/*==============================================================*/
create table store_information
(
	 no                   int not null,
   store_name           varchar(20),
   sales                decimal(10,2),
   date                 date,
   primary key (no)
);

/*==============================================================*/
/* Table: geography                                             */
/*==============================================================*/
create table geography
(
	 no										int not null,
   region_name          varchar(20),
   store_name           varchar(20),
   primary key (no)
);

delete from store_information;
delete from geography;

insert store_information(no, store_name, sales, date) values(1, "Los Angeles", 150.2, '2008.12.01');
insert store_information(no, store_name, sales, date) values(2, "San Diego", 250.3, '2008.01.01');
insert store_information(no, store_name, sales, date) values(3, "Los Angeles", 20.2, '2008.02.01');
insert store_information(no, store_name, sales, date) values(4, "Boston", 700.2, '2008.06.11');

insert geography(no, region_name, store_name) values(1, "East", "Boston");
insert geography(no, region_name, store_name) values(2, "East", "New York");
insert geography(no, region_name, store_name) values(3, "West", "Los Angeles");
insert geography(no, region_name, store_name) values(4, "West", "San Diego");

select * from store_information;
select * from geography;


mysql> select * from store_information;
+----+-------------+--------+------------+
| no | store_name  | sales  | date       |
+----+-------------+--------+------------+
|  1 | Los Angeles | 150.20 | 2008-12-01 |
|  2 | San Diego   | 250.30 | 2008-01-01 |
|  3 | Los Angeles |  20.20 | 2008-02-01 |
|  4 | Boston      | 700.20 | 2008-06-11 |
+----+-------------+--------+------------+
4 rows in set (0.00 sec)

mysql> select * from geography;
+----+-------------+-------------+
| no | region_name | store_name  |
+----+-------------+-------------+
|  1 | East        | Boston      |
|  2 | East        | New York    |
|  3 | West        | Los Angeles |
|  4 | West        | San Diego   |
+----+-------------+-------------+



select a1.region_name region, a2.store_name, a2.sales sales 
from geography a1, store_information a2 
where a1.store_name = a2.store_name;

select a1.region_name region, a2.store_name, a2.sales sales 
from geography a1 inner join store_information a2 
on a1.store_name = a2.store_name;

+--------+-------------+--------+
| region | store_name  | sales  |
+--------+-------------+--------+
| West   | Los Angeles | 150.20 |
| West   | San Diego   | 250.30 |
| West   | Los Angeles |  20.20 |
| East   | Boston      | 700.20 |
+--------+-------------+--------+

/* 每一区 (region_name) 的营业额 (sales) */
select a1.region_name region, sum(a2.sales) sales 
from geography a1, store_information a2 
where a1.store_name = a2.store_name 
group by a1.region_name

/* 每一区 (region_name) 的营业额 (sales) */
select a1.region_name region, sum(a2.sales) sales 
from geography a1 inner join store_information a2 
on a1.store_name = a2.store_name 
group by a1.region_name

+--------+--------+
| region | sales  |
+--------+--------+
| East   | 700.20 |
| West   | 420.70 |
+--------+--------+


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值