GBase 8s 连接查询使用说明

📅 2026/7/2 11:45:23 👁️ 阅读次数 📝 编程学习
GBase 8s 连接查询使用说明

在数据库应用开发中,多表关联查询是最常用的操作之一。本文将结合具体示例,介绍GBase 8s(gbase database)中的自连接、内连接、外连接及多表连接的使用方法,帮助您快速掌握各类关联查询技巧。

测试数据准备
本文使用以下三张表进行演示:

内连接 (join on)


1) 说明:

内连接只返回两个表中与连接谓词匹配的行,不匹配的行不会被输出。

2) 举例

  • 问题:查询每位顾客的联系方式及其订货日期
  • 分析:顾客信息存放在customer表中,订单日期存放在orders表中,本查询需要涉及两个表,两个表之间的联系是通过customer表的主键customer_num和orders表的外键customer_num实现的。
  • 查询语句:
    select customer.customer_num, cname, phone, order_num, order_date
    from customer join orders
    on customer.customer_num=orders.customer_num;
  • 查询结果:

外连接

内连接操作的结果只输出两个表中在连接谓词上匹配的行。如果希望连接操作将左表或者右表中不匹配的行也输出,就需要使用外连接了。外连接分为左外连接、右外连接和完全外连接。


2.1 左外连接(left outer join on)


1)说明:

左外连接会返回左表中全部的行,即使右表中没有找到匹配的行。如果左表中的行在右表中没有匹配的行,对应的行上来自右表的属性为空值。

2)举例:

  • 问题:查询每位顾客的订购物品的信息,目前没有订单的顾客也要列出
  • 分析:顾客信息在customer表中,物品的信息在orders表中,两个表通过customer表中的主键customer_num和orders表中的外键customer_num连接。
  • 查询语句:
    select customer.customer_num, cname, phone, order_num,order_date
    from customer left outer join orders
    on customer.customer_num=orders.customer_num;


查询结果:

2.2 右外连接(right outer join on)

1)说明:

右外连接会返回左表中全部的行,即使左表中没有找到匹配的行。如果右表中的行在左表中没有匹配的行,对应的行上来自左表的属性为空值。

2)举例:(与左连接类似,不再列举)

  • 问题:查询每位顾客的订购物品的信息,目前没有订单的顾客也要列出
  • 分析:顾客信息在customer表中,物品的信息在orders表中,两个表通过customer表中的主键customer_num和orders表中的外键customer_num连接。
  • 查询语句:(使用右外连接,修改左右表位置即可)
    select customer.customer_num, cname, phone, order_num,order_date
    from orders left outer join customer
    on orders.customer_num=customer.customer_num;

    查询结果:




    自连接(self-join)


1) 说明:
自连接是指一个表与自身进行连接操作,它是特殊形式的内连接。

2) 举例:

  • 问题:查询提交了一次以上订单的顾客信息
  • 分析: 在订单表orders中查看拥有不同的订单的同一个顾客的信息,将customer_num输出。
    该例中将orders表按照customer_num属性等值进行了连接,由于连接的两个表名字相同,无法使系统区分何时调用哪张表,因此需要为两张表取别名A和B ,按照要在同一行中出现不同的订单号,就说明该顾客至少签订了两个订单,通过A.order_num<>B.order_num条件可以过滤掉同一行订单记录自身匹配的情况。
  • 查询语句:
    select distinct A.customer_num
    from orders A inner join orders B on A.customer_num=B.customer_num
    where A.order_num<>B.order_num;

select distinct A.customer_num
from orders A, orders B
where A.customer_num=B.customer_num and A.order_num <>B.order_num;

或(不用连接查询,使用嵌套查询)

select A.customer_num, count(*)
from orders
group by customer_num having count(*) >1;

  • 查询结果:

多表连接


1)说明:


将两个以上的表进行连接的操作称为多表连接。连接多张表需要使用多个join关键字。可以将多表连接的执行过程理解为:第1张表与第2张表连接,将得到的中间结果表再与第3张表连接,依此类推,直到获得最终结果表。


2)举例:

  • 问题:查询每位顾客提交订单的日期、所定的物品名称及物品价格
  • 分析:由于内连接运算满足交换律和结合律,多张表的连接顺序不会改变结果表,所以DBMS不一定按照表的先后顺序依次执行连接,而是根据执行代价选择最优的连接顺序。

  • 查询语句:
    select cname, order_date, ship_name, (ship_price * quantity) as total_price
    from customer join orders
    on customer.customer_num=orders.customer_num join items on orders.order_num=items.order_num;

    select cname, order_date, ship_name, (ship_price * quantity) as total_price
    from customer , orders , items
    where customer.customer_num=orders.customer_num and orders.order_num=items.order_num;
  • 查询结果:

掌握多表关联查询是GBase 8s SQL开发的基础技能。建议根据实际业务需求选择合适的连接类型,同时注意:
1. 明确连接条件,避免遗漏导致笛卡尔积
2. 合理使用外连接,确保数据完整性
3. 多表连接时,优先考虑JOIN ON语法,逻辑更清晰,也便于维护
希望本文能帮助大家更好地理解和运用GBase 8s的多表查询功能!