繁体   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