连接表并显示结果[Laravel 5]

时间:2015-05-28 13:15:27

标签: php mysql 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 |
+----+----------+---------------+---------------------+---------------------+

我想要的是显示如下内容:

第1组

PRVO PITANJE?

DRUGO PITANJE?

但也要检查用户3是否从数据透视表中记录并仅显示该用户。我认为它应该是一些JOIN project_group和问题表,然后放入where user_id = Atuh::id()

任何解决方案? :)

1 个答案:

答案 0 :(得分:1)

首先,id上的groups应与group_id上的project_group一致,对吧?所以,我假设' GROUP1'你的身份是115,而不是1。

其次,问题与项目有何关系?您对问题有group_id,这似乎应该是项目ID,但这并不确定。

SELECT text
来自questions
INNER JOIN project_group使用(group_id
INNER JOIN groups ON project_groupproject_id = groupsid
groupsname =' GROUP1'
project_groupuser_id = 3;

同样,这只有在您将group_id更正为引用id表上的groups的外键时才有效。在这种情况下,这应该做你想要的。