java中 两个表之间有2个外键约束_从两个表中选择具有两个外键的多对多关系表中的行...

我很抱歉我的头衔不好,因为我很难选择冠军头衔 . 但我希望描述可以解释它 . 我想从关系表中选择具有两个表的外键的值虽然关系表的数据为空,所以有我的表

我有三张 table :

Student

==========

id || name

===============

1 || Rooney

2 || Carrick

3 || Smalling

4 || De Gea

然后 :

Item

==========

id || Title

===============

1 || Pre-Test

2 || Post-Test

3 || Final-Test

然后是一个关于多对多关系的表

Score

==========

id || student_id || item_id || Score

=====================================

1 || 1 || 1 || 100

2 || 1 || 2 || 80

3 || 2 || 1 || 90

4 || 2 || 3 || 85

5 || 3 || 2 || 80

6 || 3 || 3 || 90

7 || 4 || 1 || 95

我希望得到这样的结果:

Result

==========

score_id || student_name || item_name || Score

================================================

1 || Rooney || Pre-Test || 100

2 || Rooney || Post-Test || 80

NULL || Rooney || Final-Test|| NULL

3 || Carrick || Pre-Test || 90

NULL || Carrick || Post-Test || NULL

4 || Carrick || Final-Test|| 85

NULL || Smalling || Pre-Test || NULL

5 || Smalling || Post-Test || 80

6 || Smalling || Final-Test|| 90

7 || De Gea || Pre-Test || 95

NULL || De Gea || Post-Test || NULL

NULL || De Gea || Final-Test|| NULL

我从另一个论坛搜索一些外部联接的教程并在我的数据库中尝试它,但查询没有显示空值 . 谢谢,

*)编辑

我尝试使用union和join进行查询,但结果没有显示空值

SELECT score.id AS score_id, student.name, item.title, score.score

FROM student

RIGHT JOIN score ON student.id = score.student_id

RIGHT JOIN item ON score.item_id = item.id

UNION

SELECT score.id AS score_id, student.name, item.title, score.score

FROM item

RIGHT JOIN score ON score.item_id = item.id

RIGHT JOIN student ON score.student_id = student.id

ORDER BY score_id

LIMIT 0 , 30

SELECT score.id AS score_id, student.name AS student_name, item.title AS item_title, score.score

FROM student

LEFT JOIN score ON student.id = score.student_id

LEFT JOIN item ON score.item_id = item.id

LIMIT 0 , 30

更改为右连接仍然没有显示空值,这是结果:

score_id || student_name || item_title || score

1 || Rooney || Pre-Test || 100

2 || Rooney || Post-Test || 80

3 || Carrick || Pre-Test || 90

4 || Carrick || Final-Test || 85

5 || Smalling || Post-Test || 80

7 || Smalling || Final-Test || 90

8 || De Gea || Pre-Test || 95

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值