首页 > 试题广场 >

Mysql(版本8.0.25)中表student_table

[单选题]
Mysql(版本8.0.25)中表student_table(id,name,birth,sex),插入如下记录:
('1004' , '张三' ,'2000-08-06' , '男');
('1009' , '李四', '2000-01-01', '男');
('1010' , '李四', '2001-01-01', '男');
('1006' , '王五', '2000-08-06' , '女');
('1008' , '张三', '2002-12-01', '女');
('1012' , '张三', '2001-12-01', '女');
('1011' , '李四', '2002-08-06' , '女');
('1013' , '赵六' ,'2000-09-06' , '男');
现有SQL:
select t1.*,t2.*
from (
select * from student_table where sex = '男' ) t1 
full join 
(select * from student_table where sex = '女')t2 
on  t1.name = t2.name ; 
如下SQL中与上述SQL实现的语义和结果一致的是()?
  • select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    inner join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    left join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    right join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name ;
  • select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    inner join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    left join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    where t2.name is null
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    right join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name ;
  • select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    inner join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    left join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    where t2.name is null
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    right join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    where t1.name is null ;
  • select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    inner join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    left join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    union all
    select t1.*,t2.*
    from (
    select * from student_table where sex = '男' ) t1
    right join
    (select * from student_table where sex = '女')t2
    on t1.name = t2.name
    where t1.name is null ;

画韦恩图就好了。full join是全连接,内连接是两圆的交集,左连接是左圆,右连接是右圆,全连接是左圆去掉交集部分+交集部分+右圆去掉交集部分。“去掉”在代码里表示出来就是XX IS NULL。找两个IS NULL就是答案。(欢迎关注我的WX公众号:且听数据说,更多数据分析内容与你分享)

编辑于 2023-04-24 23:04:23 回复(6)
一个代码写那么长,看的脑壳子疼
发表于 2022-02-09 16:06:59 回复(2)
题目是full join  一个全连接
后面的ABCD选项意思是要和题目表达同一个意思
其实就是如果通过Union all 来表达出全连接的含义
A 选项inner join (内连接) union all  left on (左连接) union all right on(右连接)   t1,t2表内连接重复的部分会出现三次
B选项inner join (内连接) union all  left on  where(左外连接) union all right on(右连接)  可恶没有看到 B选项右连接的时候没有有where,选错了,呜呜呜
C选项inner join (内连接) union all  left on  where(左外连接) union all right on where(右外连接)  刚好符合一个全连接
D选项inner join (内连接) union all  left on  (左连接) union all right on where(右外连接)  跟B选项相反

发表于 2021-12-16 15:18:07 回复(2)

编辑不易,看懂请点赞o((>ω< ))o

id name birth sex
1004 张三 2000-08-06
1009 李四 2000-01-01
1010 李四 2001-01-01
1006 王五 2000-08-06
1008 张三 2002-12-01
1012 张三 2001-12-01
1011 李四 2002-08-06
1013 赵六 2000-09-06
select * from student_table where sex = '男'
id name birth sex
1004 张三 2000-08-06
1009 李四 2000-01-01
1010 李四 2001-01-01
1013 赵六 2000-09-06
select * from student_table where sex = '女'
id name birth sex
1006 王五 2000-08-06
1008 张三 2002-12-01
1012 张三 2001-12-01
1011 李四 2002-08-06

SQL FULL JOIN结合的左,右外连接的结果。
连接表将包含的所有记录来自两个表,并使用NULL值作为两侧缺失匹配结果

select t1.*,t2.*
from (
select * from student_table where sex = '男' ) t1 
full join 
(select * from student_table where sex = '女')t2 
on t1.name = t2.name ;

图片说明

id name birth sex id name birth sex
1004 张三 2000-08-06 1008 张三 2002-12-01
1004 张三 2000-08-06 1012 张三 2001-12-01
1009 李四 2000-01-01 1011 李四 2002-08-06
1010 李四 2001-01-01 1011 李四 2002-08-06
1013 赵六 2000-09-06 NULL NULL NULL NULL
NULL NULL NULL NULL 1006 王五 2000-08-06

如数据库不支持FULL JOIN,如MySQL不支持FULL JOIN,那么可以使用UNION ALL子句

分别拆解inner join和left join进行举例,right join同left,省略。

select *
from (
select * from student_table where sex = '男' ) t1 
inner join 
(select * from student_table where sex = '女')t2 
on  t1.name = t2.name 

inner join效果

select t1.*,t2.*
from (
select * from student_table where sex = '男' ) t1 
left join 
(select * from student_table where sex = '女')t2 
on  t1.name = t2.name 
where t2.name is null ;

left join where

最后将每一个部分用union all连接

发表于 2022-04-17 17:14:26 回复(1)
嗯嗯,很清楚;图片来自B站 康师傅的MySql教程✌
发表于 2022-06-17 17:03:09 回复(3)
班级和学生之间是________的关系
A
一对多
B
多对多
C
一对一
D
多对一
正确答案:D
你的答案:A
官方解析:一个学生只能对应一个班级,但一个班级能对应多个学生。所以是多对一
这啥情况呀?
班级是一,学生是多,不应该是一对多吗?
而且题目里面问的是班级与学生而不是学生与班级呀!
发表于 2023-06-16 11:10:30 回复(1)
这题真难搞
SELECT
	t1.*,
	t2.* 
FROM
	( SELECT * FROM student_table WHERE sex = '男' ) t1
	INNER JOIN ( SELECT * FROM student_table WHERE sex = '女' ) t2 ON t1.NAME = t2.NAME UNION ALL
SELECT
	t1.*,
	t2.* 
FROM
	( SELECT * FROM student_table WHERE sex = '男' ) t1
	LEFT JOIN ( SELECT * FROM student_table WHERE sex = '女' ) t2 ON t1.NAME = t2.NAME 
WHERE
	t2.NAME IS NULL UNION ALL
SELECT
	t1.*,
	t2.* 
FROM
	( SELECT * FROM student_table WHERE sex = '男' ) t1
	RIGHT JOIN ( SELECT * FROM student_table WHERE sex = '女' ) t2 ON t1.NAME = t2.NAME 
WHERE
	t1.NAME IS NULL;


编辑于 2023-02-24 21:05:18 回复(0)
Bryce230的解答很精辟
发表于 2022-03-13 22:22:56 回复(0)
UNION ALL 语***列出所有重复的值。而union不会
发表于 2024-05-05 10:15:52 回复(0)
 可是Mysql(版本8.0.25)不支持full join啊!
发表于 2023-02-17 14:53:24 回复(2)