<a href="#a01">点击复制data.sql数据库</a>
关键字:inner join on
示例语句:select a inner join b on a.name=b.name
说明:组合两个表中的记录,返回关联字段相符的记录,也就是返回两个表的交集部分。

关键字:left join on / left outer join on
语句:select * from a left join b on a.name=b.name
说明: left join 是left outer join的简写,它的全称是左外连接,是外连接中的一种。 左(外)连接,左表的记录将会全部表示出来,而右表只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。即相当于搜索左表的差集

关键字:right join on / right outer join on
语句:select * from a right join b on a.name=b.name
说明:right join是right outer join的简写,它的全称是右外连接,是外连接中的一种。与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。

关键字:union /union all (这里只介绍union all)
语句:1,select from a where a.id<3 union all select from b where b.id>1
2,select from a where a.id<3 union all select from b where b.id>1 order by id desc
说明:说白了,就是两个sql 语句合起来,即求并集


<div id="a01">
-- phpMyAdmin SQL Dump-- version phpStudy 2014-- http://www.phpmyadmin.netSET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";SET time_zone = "+00:00";-- 数据库: `a`CREATE DATABASE `a` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `a`;-- 表的结构 `a`CREATE TABLE IF NOT EXISTS `a` (`id` int(11) NOT NULL,`name` text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;-- 转存表中的数据 `a`INSERT INTO `a` (`id`, `name`) VALUES(1, '张三'),(2, '李四'),(3, '王二麻子'),(4, '玻璃杯子'),(5, '小红'),(4, '小明');-- ---------------------------------------------------------- 表的结构 `b`CREATE TABLE IF NOT EXISTS `b` (`id` int(11) NOT NULL,`name` text NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8;-- 转存表中的数据 `b`INSERT INTO `b` (`id`, `name`) VALUES(1, '张三'),(2, '李四'),(3, '王二麻子'),(4, '玻璃杯子'),(5, '花花'),(6, '站站');
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号