将一个值标签的单列与SQL的另一个表中的多列相结合到值的多列
我有两个表:首选项
,其中显示了学生的用餐偏好, key
,这是了解每个用餐项目的每个值的关键。
Preferences:
| Student_ID | Student_Name | Meat | Vegetable | Drink | Dessert |
|------------|--------------|------|-----------|-------|---------|
| 1 | Jeff | 3 | 1 | 4 | 1 |
| 2 | Andrea | 1 | 1 | 3 | 1 |
| 3 | Allison | 3 | 2 | 7 | 1 |
| 4 | Randy | 1 | 1 | 4 | 2 |
| 5 | Carl | 2 | 5 | 2 | 2 |
| 6 | Bobby | 1 | 6 | 7 | 2 |
| 7 | Julie | 3 | 5 | 2 | 1 |
| 8 | Anna | 1 | 6 | 7 | 2 |
| 9 | Carlos | 1 | 6 | 2 | 2 |
| 10 | Roger | 2 | 4 | 2 | 1 |
| 11 | Pierre | 1 | 2 | 1 | 1 |
| 12 | Troy | 2 | 3 | 3 | 1 |
| 13 | David | 3 | 6 | 6 | 2 |
| 14 | Michaela | 1 | 4 | 5 | 2 |
| 15 | Rose | 1 | 4 | 6 | 1 |
| 16 | Anita | 3 | 6 | 6 | 2 |
| 17 | Connor | 3 | 3 | 3 | 1 |
| 18 | Eddie | 1 | 2 | 7 | 1 |
| 19 | Karen | 3 | 5 | 5 | 2 |
| 20 | Rachel | 3 | 2 | 2 | 1 |
| | | | | | |
Key:
| Item_Type | Item | Value |
|-----------|----------|-------|
| Meat | Chicken | 1 |
| Meat | Beef | 2 |
| Meat | Fish | 3 |
| Vegetable | Carrots | 1 |
| Vegetable | Peas | 2 |
| Vegetable | Corn | 3 |
| Vegetable | Broccoli | 4 |
| Vegetable | Zucchini | 5 |
| Vegetable | Eggplant | 6 |
| Drink | Water | 1 |
| Drink | Milk | 2 |
| Drink | Juice | 3 |
| Drink | Cola | 4 |
| Drink | Lemonade | 5 |
| Drink | Tea | 6 |
| Drink | Punch | 7 |
| Dessert | Cake | 1 |
| Dessert | Pie | 2 |
我正在尝试找出首选项中的每一列项目值的价值标签的最有效方法
从
key item
列。 >。换句话说,我想加入
两个表以创建下表:
| Student_ID | Student_Name | Meat | Vegetable | Drink | Dessert |
|------------|--------------|---------|-----------|----------|---------|
| 1 | Jeff | Fish | Carrots | Cola | Cake |
| 2 | Andrea | Chicken | Carrots | Juice | Cake |
| 3 | Allison | Fish | Peas | Punch | Cake |
| 4 | Randy | Chicken | Carrots | Cola | Pie |
| 5 | Carl | Beef | Zucchini | Milk | Pie |
| 6 | Bobby | Chicken | Eggplant | Punch | Pie |
| 7 | Julie | Fish | Zucchini | Milk | Cake |
| 8 | Anna | Chicken | Eggplant | Punch | Pie |
| 9 | Carlos | Chicken | Eggplant | Milk | Pie |
| 10 | Roger | Beef | Broccoli | Milk | Cake |
| 11 | Pierre | Chicken | Peas | Water | Cake |
| 12 | Troy | Beef | Corn | Juice | Cake |
| 13 | David | Fish | Eggplant | Tea | Pie |
| 14 | Michaela | Chicken | Broccoli | Lemonade | Pie |
| 15 | Rose | Chicken | Broccoli | Tea | Cake |
| 16 | Anita | Fish | Eggplant | Tea | Pie |
| 17 | Connor | Fish | Corn | Juice | Cake |
| 18 | Eddie | Chicken | Peas | Punch | Cake |
| 19 | Karen | Fish | Zucchini | Lemonade | Pie |
| 20 | Rachel | Fish | Peas | Milk | Cake |
| | | | | | |
peferences中每个学生的每个进餐项目值
匹配从 key
中的单个 item
列中获得其适当的标签。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定
您提到的最有效的方法
,但是您可以执行Inner Join
四次,如下:请参阅 db-fiddle 。
I'm not sure about
the most efficient way
as you mentioned, but you can perform aninner join
four times as the following:See demo from db-fiddle.
这是一个带有单个连接的解决方案。
它是在MySQL中实现的,但是有了非常小的更改,它可以在任何其他DB中实现。
Here's a solution with a single join.
It was implemented in MySQL, but with very slight changes it can be implemented in any other DB.
Fiddle
这是一个基于 undivot&枢轴。
请注意,仅对于枢轴语句才需要子查询,因为该语句一部分的任何列都成为隐式组的一部分。
小提琴
Here is a full, very lean SQL Server solution based on unpivot & pivot.
Please note that subquery is needed only for the pivot statement since any column that isn't part of that statement becomes part of an implicit group by.
Fiddle
另一种方法是进行两项操作,首先是a undivot - 然后是内部联接。不确定您是否想再次旋转结果,但是如果您这样做,我想这将是第三个操作。
Another way is to do two operations, first a unpivot - and then an inner join. Not sure if you'd want to pivot the result again, but if you did I guess that would be a third operation.