多表查询笔记---内连接查询

MySQL多表查询语句笔记

内连接查询,可以有效的去除笛卡尔积现象

隐式内连接

select * from A,B where 条件隐式连接使用别名:select * from A 别名1,B 别名2 where 别名1.xx=别名2.xx;
例:现在有两张表products和orderitem表,
products表中的数据:
+--------------------------------------+--------------------------+-------+----------+------+-----------------------------+--------------------------------------------------------------+
| id                                   | name                     | price | category | pnum | imgurl                      | description                                                  |
+--------------------------------------+--------------------------+-------+----------+------+-----------------------------+--------------------------------------------------------------+
| 04e3e416-001c-4933-8490-d1c7fdfdd5cd | 深入理解计算机系统       |   102 | 计算机   |   80   | NULL                        | 深入理解计算机系统--计算机经典书籍                           |
| 5aef5688-6b7a-4d6c-aec7-dd09aa06de30 | C++ Primer Plus          |    90 | 计算机   |   80 | cppPrimerPlus.jpg           | c++ primer plus                                              |
| 617c81fe-2a85-40fd-9304-b875d2450979 | 童年                     |    50 | 文学     |   80 | 童年.jpg                    | 该作讲述了阿廖沙(高尔基的乳名)三岁到十岁这一时期的童年生活 |
| 72c52302-cd1e-4a22-8ac8-dc300a915be5 | 计算机导论               |    59 | 计算机   |    3 |                             | 计算机导论                                                   |
| 79bbe618-d2f8-4081-b35a-62ebbe938b64 | 计算机网络               |  44.5 | 计算机   |   55 |                             | 计算机网络                                                   |
| 84c842da-16b6-4e87-953e-859a1ca62bab | 软件工程                 |    89 | 计算机   |   44 |                             | 软件工程                                                     |
| 8f597392-0f5b-4613-8398-476182ee6e1f | Lewin 基因X(中文版)    |   298 | 科技     |   80 | 基因X.jpg                   | 内容涵盖了基因的结构、序列、组织和表达。                     |
| a7fa37ad-b3db-4676-8c68-745deba3b4df | 天才在左,疯子在右       |  19.8 | 社科     |   80 | tiancai.jpg                 | 精神病人的世界                                               |
| b911429c-667e-4600-85b4-9bcb8a5f7a34 | 计算机网络:自顶向下方法 |    67 | 计算机   |   80 | 计算机网络-自顶向下方法.jpg | 计算机网络:自顶向下方法--计算机经典书籍                     |
| eb84d046-c5aa-4d21-8995-6ab901aa519a | 三国演义                 |    45 | 文学     |   80 | 三国演义.jpg                | 三国演义--罗贯中                                             |
+--------------------------------------+--------------------------+-------+----------+------+-----------------------------+--------------------------------------------------------------+
orderitem表中的数据:
+--------------------------------------+--------------------------------------+--------+
| order_id                             | product_id                           | buynum |
+--------------------------------------+--------------------------------------+--------+
| 0c0796f2-0124-4a13-a891-5efbb63b04f9 | 79bbe618-d2f8-4081-b35a-62ebbe938b64 |     10 |
| 305a7870-3820-4079-b6f9-5d2b63cbcd2a | 72c52302-cd1e-4a22-8ac8-dc300a915be5 |      2 |
| 611f80fa-4273-4674-be09-9530b6276e15 | 84c842da-16b6-4e87-953e-859a1ca62bab |     11 |
+--------------------------------------+--------------------------------------+--------+
隐式内连接查询,查询产品的名称及其销售数量
 select * from products p, orderitem oi where oi.product_id=p.id;

查询结果:

+--------------------------------------+------------+-------+----------+------+--------+-------------+--------------------------------------+--------------------------------------+--------+
| id                                   | name       | price | category | pnum | imgurl | description | order_id                             | product_id                           | buynum |
+--------------------------------------+------------+-------+----------+------+--------+-------------+--------------------------------------+--------------------------------------+--------+
| 79bbe618-d2f8-4081-b35a-62ebbe938b64 | 计算机网络 |  44.5 | 计算机   |   55 |        | 计算机网络  | 0c0796f2-0124-4a13-a891-5efbb63b04f9 | 79bbe618-d2f8-4081-b35a-62ebbe938b64 |     10 |
| 72c52302-cd1e-4a22-8ac8-dc300a915be5 | 计算机导论 |    59 | 计算机   |    3 |        | 计算机导论  | 305a7870-3820-4079-b6f9-5d2b63cbcd2a | 72c52302-cd1e-4a22-8ac8-dc300a915be5 |      2 |
| 84c842da-16b6-4e87-953e-859a1ca62bab | 软件工程   |    89 | 计算机   |   44 |        | 软件工程    | 611f80fa-4273-4674-be09-9530b6276e15 | 84c842da-16b6-4e87-953e-859a1ca62bab |     11 |
+--------------------------------------+------------+-------+----------+------+--------+-------------+--------------------------------------+--------------------------------------+--------+
隐式内连接查询,查询产品的名称及其销售数量,按销量来排序
select name , buynum from  products p, orderitem oi where oi.product_id=p.id order by buynum;

order by 关键字默认是升序排序的
所以结果显示如下:

+------------+--------+
| name       | buynum |
+------------+--------+
| 计算机导论 |      2  |
| 计算机网络 |     10  |
| 软件工程   |     11  |
+------------+--------+

如果想要降序排序的话,要添加关键字 desc

select name , buynum from  products p, orderitem oi where oi.product_id=p.id order by buynum desc;

结果显示如下:

+------------+--------+
| name       | buynum |
+------------+--------+
| 软件工程   |     11 |
| 计算机网络 |     10 |
| 计算机导论 |      2 |
+------------+--------+

显式内连接

select * from A inner join B on 条件 (inner可以省略)显示连接使用别名: select * from A 别名1 inner join B 别名2 on 别名1.xx=别名2.xx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值