假設我有 2 個表,例如:
table_a
student teacher
A Z
B Z
C Z
table_b
id person
1 A
2 B
3 C
4 Z
我正在尋找以下輸出:
table_c
student_id teacher_id
1 4
2 4
3 4
我一直在嘗試解決這個問題,但想不出一個簡單的方法,并相信我想多了。我很困惑如何將值加入student并teacher與person得到我的輸出。我通常將我嘗試過的代碼附加到我的帖子中,但老實說在這里畫空白。
uj5u.com熱心網友回復:
您可以嘗試使用兩個相關子查詢來實作。
SELECT (SELECT b.id FROM table_b b WHERE b.person = a.student LIMIT 1) student_id,
(SELECT b.id FROM table_b b WHERE b.person = a.teacher LIMIT 1) teacher_id
FROM table_a a
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/428230.html
