联接表并显示结果[Laravel 5] [英] join tables and display result [Laravel 5]

查看:61
本文介绍了联接表并显示结果[Laravel 5]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有groupsproject_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆