如何实现MySQL中涉及多表的联合查询操作?

2026-05-05 23:391阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计768个文字,预计阅读时间需要4分钟。

如何实现MySQL中涉及多表的联合查询操作?

创建用户表,表结构如下:sqlCREATE TABLE user ( id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(10) DEFAULT 'xxx', sex ENUM('w', 'm') DEFAULT 'w', age TINYINT(4) NULL DEFAULT 18, grade VARCHAR(10) DEFAULT 'python');

多表联查 1、创建表
  • user表

    CREATE TABLE user ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT , `username` varchar(10) DEFAULT 'xxx' , `sex` enum('w','m') DEFAULT 'w' , `age` tinyint(4) NULL DEFAULT 18 , `grade` varchar(10) DEFAULT 'python36' , PRIMARY KEY (`id`) );

  • address表

    CREATE TABLE address ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT , `uid` int(10) UNSIGNED NOT NULL , `address` varchar(255) DEFAULT NULL , PRIMARY KEY (`id`) );

2、隐式内连接
  • 主体结构

    select * from 表名1,表名2... where 表名1.字段=表名2.字段 and ...

    查询id为1的用户的 用户信息以及地址信息

    select * from user where id=1;

    select * from address where uid=1;

  • 实例

    不起别名查询所有

    SELECT * FROM USER, address WHERE `user`.id = address.uid AND `user`.id = 1;

    不起别名查询某个字段

    SELECT `user`.id, `user`.username, address.address FROM USER, address WHERE `user`.id = address.uid AND `user`.id = 1;

    起别名查询某个字段

    SELECT u.id, u.username, a.address FROM USER u, address a WHERE u.id = a.uid AND u.id = 1;

3、显示内连接 inner join
  • 主体结构

    如何实现MySQL中涉及多表的联合查询操作?

    select * from 表1 inner join 表2 on 条件;

  • 查询id为1的用户的 用户信息以及地址信息

    不起别名

    SELECT * FROM USER INNER JOIN address ON `user`.id = address.uid AND `user`.id = 1;

    起别名

    select * from user u INNER JOIN address a on u.id=a.uid and u.id=1;

4、左连接 left join
  • 主体

    select * from 表1 left join 表2 on 条件...

  • 查询id为1的用户的 用户信息以及地址信息

    不起别名

    select * from user LEFT JOIN address on `user`.id=address.uid and `user`.id=1;

    起别名

    select * from user u LEFT JOIN address a on u.id=a.uid and u.id=1;

5、右连接 right join
  • 主体

    select * from 表1 rightjoin 表2 on 条件...

  • 查询id为1的用户的 用户信息以及地址信息

    不起别名

    select * from user RIGHT JOIN address on `user`.id=address.uid and `user`.id=1;

    起别名

    select * from user u RIGHT JOIN address a on u.id=a.uid and u.id=1;

6、注意
  • 显示内连接和隐式内连接是相同的 只把关联的数据查询出来
  • 左连接 会以左表为主表 右表为辅表 把主表的数据全部查询出来 辅表中如果没有关联的数据 则使用null来占位
  • 右连接 会以右表为主表 左表为辅表 把主表的数据全部查询出来 辅表中如果没有关联的数据 则使用null来占位
  • 建议使用隐式内连接

本文共计768个文字,预计阅读时间需要4分钟。

如何实现MySQL中涉及多表的联合查询操作?

创建用户表,表结构如下:sqlCREATE TABLE user ( id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(10) DEFAULT 'xxx', sex ENUM('w', 'm') DEFAULT 'w', age TINYINT(4) NULL DEFAULT 18, grade VARCHAR(10) DEFAULT 'python');

多表联查 1、创建表
  • user表

    CREATE TABLE user ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT , `username` varchar(10) DEFAULT 'xxx' , `sex` enum('w','m') DEFAULT 'w' , `age` tinyint(4) NULL DEFAULT 18 , `grade` varchar(10) DEFAULT 'python36' , PRIMARY KEY (`id`) );

  • address表

    CREATE TABLE address ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT , `uid` int(10) UNSIGNED NOT NULL , `address` varchar(255) DEFAULT NULL , PRIMARY KEY (`id`) );

2、隐式内连接
  • 主体结构

    select * from 表名1,表名2... where 表名1.字段=表名2.字段 and ...

    查询id为1的用户的 用户信息以及地址信息

    select * from user where id=1;

    select * from address where uid=1;

  • 实例

    不起别名查询所有

    SELECT * FROM USER, address WHERE `user`.id = address.uid AND `user`.id = 1;

    不起别名查询某个字段

    SELECT `user`.id, `user`.username, address.address FROM USER, address WHERE `user`.id = address.uid AND `user`.id = 1;

    起别名查询某个字段

    SELECT u.id, u.username, a.address FROM USER u, address a WHERE u.id = a.uid AND u.id = 1;

3、显示内连接 inner join
  • 主体结构

    如何实现MySQL中涉及多表的联合查询操作?

    select * from 表1 inner join 表2 on 条件;

  • 查询id为1的用户的 用户信息以及地址信息

    不起别名

    SELECT * FROM USER INNER JOIN address ON `user`.id = address.uid AND `user`.id = 1;

    起别名

    select * from user u INNER JOIN address a on u.id=a.uid and u.id=1;

4、左连接 left join
  • 主体

    select * from 表1 left join 表2 on 条件...

  • 查询id为1的用户的 用户信息以及地址信息

    不起别名

    select * from user LEFT JOIN address on `user`.id=address.uid and `user`.id=1;

    起别名

    select * from user u LEFT JOIN address a on u.id=a.uid and u.id=1;

5、右连接 right join
  • 主体

    select * from 表1 rightjoin 表2 on 条件...

  • 查询id为1的用户的 用户信息以及地址信息

    不起别名

    select * from user RIGHT JOIN address on `user`.id=address.uid and `user`.id=1;

    起别名

    select * from user u RIGHT JOIN address a on u.id=a.uid and u.id=1;

6、注意
  • 显示内连接和隐式内连接是相同的 只把关联的数据查询出来
  • 左连接 会以左表为主表 右表为辅表 把主表的数据全部查询出来 辅表中如果没有关联的数据 则使用null来占位
  • 右连接 会以右表为主表 左表为辅表 把主表的数据全部查询出来 辅表中如果没有关联的数据 则使用null来占位
  • 建议使用隐式内连接