联接表并显示结果[Laravel 5] [英] join tables and display result [Laravel 5]
问题描述
我有groups
表project_group
数据透视表questions
表
groups
表:
+----+--------+---------------------+---------------------+
| id | name | updated_at | created_at |
+----+--------+---------------------+---------------------+
| 1 | GROUP1 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
| 2 | GROUP2 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+--------+---------------------+---------------------+
数据透视表project_group
:
+-----+----------+------------+----------+---------+-----------+
| ids | group_id | project_id | admin_id | user_id | user_name |
+-----+----------+------------+----------+---------+-----------+
| 4 | 115 | 1 | 1 | 3 | CLIENT |
| 5 | 115 | 2 | 1 | 3 | CLIENT |
+-----+----------+------------+----------+---------+-----------+
问题表:
+----+----------+---------------+---------------------+---------------------+
| id | group_id | text | updated_at | created_at
+----+----------+---------------+---------------------+---------------------+
| 1 | 115 | PRVO PITANJE? | 2015-05-03 14:29:16 | 2015-05-03 14:29:16 |
| 26 | 115 | DRUGO PITANJE? | 2015-05-06 00:44:02 | 0000-00-00 00:00:00 |
+----+----------+---------------+---------------------+---------------------+
我要显示的是这样的内容:
What I want is to display something like this:
PRVO PITANJE?
PRVO PITANJE?
DRUGO PITANJE?
DRUGO PITANJE?
还可以检查用户3是否从数据透视表登录,并仅显示该用户的信息.我认为应该是一些JOIN project_group和问题表,然后放入where user_id = Atuh::id()
BUT also to check if user 3 logged from pivot table and display that just for that user. I think it should be some JOIN project_group and question table and then to put where user_id = Atuh::id()
有解决方案吗? :)
推荐答案
首先,您在groups
上的id
应该与project_group
上的group_id
对齐,对吗?因此,我假设"GROUP1"的ID为115,而不是1.
Firstly, your id
on groups
ought to align with group_id
on project_group
, right? So, I'll assume 'GROUP1' has id of 115 and not 1 as you have it.
第二,问题与项目有什么关系?您对问题有group_id
的提示,似乎应该是项目ID,但这并不确定.
Secondly, how do questions relate to projects? You have group_id
on questions, which seems like it should be a project id, but this isn't certain.
选择text
从questions
内联接project_group
使用(group_id
)
内联接groups
ON project_group
.project_id
= groups
.id
groups
.name
='GROUP1'
AND project_group
.user_id
= 3;
SELECT text
FROM questions
INNER JOIN project_group
USING (group_id
)
INNER JOIN groups
ON project_group
.project_id
=groups
.id
WHERE groups
.name
='GROUP1'
AND project_group
.user_id
=3;
同样,仅当您将group_id
纠正为引用groups
表上的id
的外键时,这才起作用.在这种情况下,这应该做您想要的.
Again, this will only work if you correct group_id
to be a foreign key referencing id
on the groups
table. In this case, this should do what you want.
这篇关于联接表并显示结果[Laravel 5]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!