[英]php, mysql join table columns with another table rows
I have issue getting results from two database table.我在从两个数据库表中获取结果时遇到问题。 here is what i have:
这是我所拥有的:
table A: 'courses'
math | history | geography | computer
1 | 2 | 3 | 4
and Table B和表 B
user_id | classroom_id | course
1 | 5 | 3
1 | 5 | 4
1 | 6 | 2
I returned the table A on a for each loop but I would like to check what courses the user 1 has to return true or false on any Table a columns.我为每个循环返回了表 A,但我想检查用户 1 必须在任何表 a 列上返回 true 或 false 的课程。 Any help appreciated.
任何帮助表示赞赏。 I need help not negative votes :(
我需要帮助而不是反对票:(
You have your database set up wrong I believe.我相信你的数据库设置错误。 What you want is something like
你想要的是类似的东西
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
Then you could do something like然后你可以做类似的事情
SELECT
TableA.PKEY,
TableA.Course,
TableB.user_id,
TableB.classroom_id,
TableB.course,
FROM TableA
LEFT JOIN TableB
ON TableA.PKEY = TableB.course
^^This will return the data from BOTH tables. ^^这将从两个表中返回数据。
you see this line你看到这条线
ON TableA.PKEY = TableB.course
^^This is called the foreign key.
BIG GOTCHA: Make SURE that the columns for both of those ^^^ are set up EXACTLY the same.大问题:确保这两个 ^^^ 的列设置完全相同。 For instance, IF TableA.PKEY is an UNSIGNED INT(10), then TableB.course MUST also be UNSIGNED INT(10).
例如,如果 TableA.PKEY 是 UNSIGNED INT(10),那么 TableB.course 也必须是 UNSIGNED INT(10)。 They have to be identical for the join to work correctly.
它们必须相同才能使连接正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.