MySQL连接查询实例详解

作者:程志伟 时间:2024-01-28 11:39:13 

本文实例讲述了MySQL连接查询。分享给大家供大家参考,具体如下:

创建表suppliers:


CREATE TABLE suppliers
(
s_id   int   NOT NULL AUTO_INCREMENT,
s_name  char(50) NOT NULL,
s_city  char(50) NULL,
s_zip   char(10) NULL,
s_call  CHAR(50) NOT NULL,
PRIMARY KEY (s_id)
) ;
INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call)
VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'),
(102,'LT Supplies','Chongqing','400000','44333'),
(103,'ACME','Shanghai','200000','90046'),
(104,'FNK Inc.','Zhongshan','528437','11111'),
(105,'Good Set','Taiyuang','030000', '22222'),
(106,'Just Eat Ours','Beijing','010', '45678'),
(107,'DK Inc.','Zhengzhou','450000', '33332');

内连接


SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;

使用 inner join 语法进行内连接查询


SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits
INNER JOIN suppliers ON fruits.s_id = suppliers.s_id;

创建表orders:


CREATE TABLE orders
(
o_num int   NOT NULL AUTO_INCREMENT,
o_date datetime NOT NULL,
c_id  int   NOT NULL,
PRIMARY KEY (o_num)
) ;
INSERT INTO orders(o_num, o_date, c_id)
VALUES(30001, '2008-09-01', 10001),
(30002, '2008-09-12', 10003),
(30003, '2008-09-30', 10004),
(30004, '2008-10-03', 10005),
(30005, '2008-10-08', 10001);

左连接


SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;

右连接


SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;

复合条件连接查询


SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;

【例.46】在fruits表和suppliers表之间使用内连接查询,查询之前,查看两个表的结构


SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;

【例.47】在fruits表和suppliers表之间使用INNER JOIN语法进行内连接查询


SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id;

【例.48】查询供应f_id='a1'的水果供应商提供的其他水果种类


SELECT f1.f_id, f1.f_name
FROM fruits AS f1, fruits AS f2
WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';

【例.49】在customers表和orders表中,查询所有客户,包括没有订单的客户,SQL语法如下


SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;

【例.50】在customers表和orders表中,查询所有订单,包括没有客户的订单


SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;

【例.51】在customers表和orders表中,使用INNER JOIN语法查询customers表中ID为10001、的客户的订单信息


SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;

【例.52】在fruits表和suppliers表之间使用INNER JOIN语法进行内连接查询,并对查询结果排序


SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;

希望本文所述对大家MySQL数据库计有所帮助。

来源:https://blog.csdn.net/c1z2w3456789/article/details/53897056

标签:MySQL,连接查询
0
投稿

猜你喜欢

  • 解决Mysql的left join无效及使用的注意事项说明

    2024-01-16 00:18:09
  • Python切换pip安装源的方法详解

    2023-04-29 22:06:36
  • Python importlib动态导入模块实现代码

    2022-07-31 05:52:25
  • ASP JSON类文件的使用方法

    2011-04-30 16:39:00
  • 聊聊python中not 与 is None的区别

    2023-01-18 22:20:47
  • 用js来解决ajax读取页面乱码

    2024-04-18 10:56:04
  • MySql如何实现远程登录MySql数据库过程解析

    2024-01-28 06:47:51
  • 用Python在Excel里画出蒙娜丽莎的方法示例

    2023-12-18 02:59:21
  • Python3.6笔记之将程序运行结果输出到文件的方法

    2023-08-02 08:27:44
  • 在Python中操作字典之update()方法的使用

    2023-01-16 09:55:53
  • python静态方法实例

    2023-02-17 12:03:22
  • asp如何检测HTTP表头信息?

    2010-06-28 18:20:00
  • CSDN 博客的代码高亮问题自己修复

    2022-07-28 11:33:25
  • Python文件操作函数用法实例详解

    2021-03-12 07:10:29
  • face_recognition库在python的安装

    2021-06-16 02:29:27
  • 如何在Windows中安装多个python解释器

    2023-08-28 02:55:43
  • Python numpy之线性代数与随机漫步

    2021-12-04 05:20:01
  • 深入学习Python+Opencv常用四种图像处理操作

    2023-02-22 12:28:27
  • Django应用程序中如何发送电子邮件详解

    2023-11-04 02:07:54
  • Python3.6 之后字典是有序的?

    2021-02-14 08:27:53
  • asp之家 网络编程 m.aspxhome.com