mysql - 在表中多次加入同一字段
问题描述
我有这 2 张桌子,我试图多次加入它们但失败了。下面是表格。
表 cca 主题:
+------------+----------+
| ccaSubject | ccaPrice |
+------------+----------+
| Chess | 100 |
| Badminton | 300 |
| Dancing | 200 |
| Singing | 200 |
| Football | 250 |
| Fitness | 600 |
| Robotics | 1000 |
+------------+----------+
表risp注册
+--------------------+-----------+-----------+----------+
| studentIdentifier | firstCCA | secondCCA | thirdCCA |
+--------------------+-----------+-----------+----------+
| elly@example.com | Robotics | Singing | Dancing |
| mike@example.com | Chess | Singing | Robotics |
| tom@example.com | Badminton | Dancing | Chess |
| peter@example.com | Football | Fitness | Robotics |
| andrew@example.com | Robotics | Singing | Chess |
+--------------------+-----------+-----------+----------+
我希望我的输出是这样的:
+--------------------+-----------+-----------+----------+-----------+-----------+-----------+
| studentIdentifier | firstCCA | secondCCA | thirdCCA | CCA1price | CCA2price | CCA3price |
+--------------------+-----------+-----------+----------+-----------+-----------+-----------+
| elly@example.com | Robotics | Singing | Dancing | 1000 | 200 | 200 |
| mike@example.com | Chess | Singing | Robotics | 100 | 200 | 1000 |
| tom@example.com | Badminton | Dancing | Chess | 300 | 200 | 100 |
| peter@example.com | Football | Fitness | Robotics | 250 | 600 | 1000 |
| andrew@example.com | Robotics | Singing | Chess | 1000 | 200 | 100 |
+--------------------+-----------+-----------+----------+-----------+-----------+-----------+
从我的代码中,我只能使用一次内部连接并获得CCA1price
,而我无法再获得cca2price
and cca3price
,因为错误一直在说Same aliases
。
解决方案
您可以根据需要多次rispEnrollment
将表格加入表格。ccaSubjects
在这种情况下,您可以加入 3 次以分别为三个主题栏引入价格栏。
SELECT
t1.studentIdentifier,
t1.firstCCA,
t1.secondCCA,
t1.thirdCCA,
t2.ccaPrice AS CCA1price,
t3.ccaPrice AS CCA2price,
t4.ccaPrice AS CCA3price
FROM rispEnrollment t1
LEFT JOIN ccaSubjects t2
ON t1.firstCCA = t2.ccaSubject
LEFT JOIN ccaSubjects t3
ON t1.secondCCA = t3.ccaSubject
LEFT JOIN ccaSubjects t4
ON t1.thirdCCA = t4.ccaSubject;
请注意,我在这里使用左连接以防rispEnrollment
表中的主题可能与表中的任何内容都不匹配ccaSubjects
。
推荐阅读
- javascript - Material-UI:如何制作对齐的表单
- r - 使用 RMariaDB 和 R 中的池清理发送到 MariaDB 的多个 JSON 字符串
- java - 如何在 Java 流处理中存储中间状态
- c++ - 将无向连通图分解为两个组件
- javascript - 数组未定义 - Codewars Kata Round Robin Tournament
- c# - 列表和集合的通用约束
- c# - 当其他控件放置在 datagrid 列中时,它没有绑定
- c# - gitignore 文件可以具有 XML 文档文件的通用模式吗?
- .net-core - CS0120 非静态字段、方法或属性“Startup.Configuration”需要对象引用
- python - ImportError:导入 PCA 时无法导入名称“LatentDirichletAllocation”