mysql的左联结与右连接
2015-09-05 22:37
519 查看
左连接where只影向右表,右连接where只影响左表。
左连接后的检索结果是显示tbl1的所有数据和tbl2中满足where
条件的数据。
左连接
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
;
右连接
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
right JOIN test2 ON test1.id = test2.id
;
全连接
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
union
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
right JOIN test2 ON test1.id = test2.id
;
选出test1中有test2中没有的
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
WHERE test2.id is NULL
;
左连接后的检索结果是显示tbl1的所有数据和tbl2中满足where
条件的数据。
左连接
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
;
右连接
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
right JOIN test2 ON test1.id = test2.id
;
全连接
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
union
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
right JOIN test2 ON test1.id = test2.id
;
选出test1中有test2中没有的
SELECT
test1.id AS id,
test1.name1 AS NAME,
test2.id AS id2,
test2.name2 AS name1
FROM
test1
left JOIN test2 ON test1.id = test2.id
WHERE test2.id is NULL
;
相关文章推荐
- MySQL sql语句总结
- MYSQL数据库学习----MYSQL数据库、表操作和引擎区别
- 安装mysql connector odbc 后在 控制面板 数据源下没有找到mysql的驱动
- MySQL中不能插入中文解决办法
- dos进去mysql导入数据库
- OS---net start mysql 发生系统错误5
- 如何利用一个数据库中间件扩展MySQL集群——kingshard使用指南
- mysql常用命令
- mysql外连接查询用法
- 使用淘宝中间件cobar实现mysql分库分表
- mysql中engine=innodb和engine=myisam的区别
- MySQL的基础命令
- c#------MySqlHelper
- MySQL数据类型和常用字段属性总结
- mysql主主模式配置
- mysql数据库用source命令导入.sql文件,执行SQL语句
- mysql的like查询默认是不区分大小写的
- MySQL赋权与回收权限命令
- mysql优化sql语句步骤
- 修改MySQL的数据库引擎为INNODB的方法