慕仙5996954
2018-04-20 10:02
都不介绍左表和右表的定义吗?找这么讲左连接和右连接是一样的我只需要把表调个位置就好了?能具体说明一下的吗
按照道理来说是这样的,但是如果你左右表写反了,改左右连接会更加方便快捷。
-- 注意自己是否需要建库
create database if not exists mysqldv1;
use mysqldv1;
-- 新建 user1 表
create table if not exists user1(
id smallint unsigned primary key auto_increment,
user_name varchar(40),
over1 varchar(40)
);
-- 新建 user2 表
create table if not exists user2 (
id smallint unsigned primary key auto_increment,
user_name varchar(40),
over1 varchar(40)
);
-- 新建 user_kills 表
create table if not exists user_kills (
id smallint unsigned primary key auto_increment,
user_id smallint unsigned,
timestr timestamp default CURRENT_TIMESTAMP,
kills smallint unsigned
);
-- 插入 user1 表数据
insert into user1(user_name,over1) values ('唐僧', '旃檀功德佛');
insert into user1(user_name,over1) values ('猪八戒', '净坛使者');
insert into user1(user_name,over1) values ('孙悟空', '斗战胜佛');
insert into user1(user_name,over1) values ('沙僧', '金身罗汉');
-- 插入 user2 表数据
insert into user2(user_name,over1) values ('孙悟空', '成佛');
insert into user2(user_name,over1) values ('牛魔王', '被降服');
insert into user2(user_name,over1) values ('蛟魔王', '被降服');
insert into user2(user_name,over1) values ('鹏魔王', '被降服');
insert into user2(user_name,over1) values ('狮驼王', '被降服');
-- 插入 user_kills 表数据
insert into user_kills(user_id, timestr, kills) values (2, timestamp('2013-01-10'), 10);
insert into user_kills(user_id, timestr, kills) values (2, timestamp('2013-02-01'), 2);
insert into user_kills(user_id, timestr, kills) values (2, timestamp('2013-02-05'), 12);
insert into user_kills(user_id, timestr, kills) values (4, timestamp('2013-01-10'), 3);
insert into user_kills(user_id, timestr, kills) values (4, timestamp('2013-02-11'), 5);
insert into user_kills(user_id, timestr, kills) values (2, timestamp('2013-02-06'), 1);
insert into user_kills(user_id, timestr, kills) values (3, timestamp('2013-01-11'), 20);
insert into user_kills(user_id, timestr, kills) values (2, timestamp('2013-02-12'), 10);
insert into user_kills(user_id, timestr, kills) values (3, timestamp('2013-02-07'), 17);
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
MySQL开发技巧(一)
148779 学习 · 123 问题
相似问题