簡體   English   中英

php,mysql 將表列與另一個表行連接起來

[英]php, mysql join table columns with another table rows

我在從兩個數據庫表中獲取結果時遇到問題。 這是我所擁有的:

table A: 'courses' 

math  | history  | geography  | computer 
1     | 2        | 3          | 4 

和表 B

user_id | classroom_id | course 
1       | 5            | 3
1       | 5            | 4
1       | 6            | 2

我為每個循環返回了表 A,但我想檢查用戶 1 必須在任何表 a 列上返回 true 或 false 的課程。 任何幫助表示贊賞。 我需要幫助而不是反對票:(

我相信你的數據庫設置錯誤。 你想要的是類似的東西

Table_A:
PKEY |    Course

1    |    Math
2    |    History
3    |    Geography
4    |    Computer



Table_B:
user_id | classroom_id | course 
1       | 5            | 3
1       | 5            | 4
1       | 6            | 2

然后你可以做類似的事情

SELECT 
TableA.PKEY,
TableA.Course,
TableB.user_id,
TableB.classroom_id,
TableB.course,
FROM TableA
LEFT JOIN TableB
ON TableA.PKEY = TableB.course

^^這將從兩個表中返回數據。

你看到這條線

ON TableA.PKEY = TableB.course

                    ^^This is called the foreign key.

大問題:確保這兩個 ^^^ 的列設置完全相同。 例如,如果 TableA.PKEY 是 UNSIGNED INT(10),那么 TableB.course 也必須是 UNSIGNED INT(10)。 它們必須相同才能使連接正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM