如何在三个表中使用查询生成器进行查询?

时间:2018-01-29 23:45:49

标签: mysql laravel laravel-5.5 builder

我有三张表,员工,技能和技术 tables

=====================
|    employe        |
=====================
|id_employe         |
|name               |
|id_skill           |


=====================
|    skills         |
=====================
|id_skill           |
|Id_technologie     |
=====================


=====================
|    technologies   |
=====================
|id_technologie     |
|name               |
=====================

在每张桌子中我都有这些 records

employe
=============================
|id_employe|name  |id_skill |
=============================
|1         |jhon  |1        |
|2         |tom   |2        |

skill
=============================
|id_skill  | id_technologie  |
=============================
|1         |          1      |
|1         |          2      |
|2         |          1      |
|2         |          4      |


technologies
============================
|id_technologie| name      |
============================
|1             |  PHP      |
|2             |  Java     |
|3             |  Python   |
|4             |  C        |

我想得到一个结果 this

=============================================
id_employe| name |  id_skill|   name  | name
=============================================
1         | jhon |     1    |    PHP  | Java

我试图这样做,但结果不是我的预期

function listar(){
    $users = DB::table('employe')
        ->join('skills', 'skills.id_skill', '=', 'employe.id_skill')
        ->join('technologies', 'technologies.id_technologie', '=', 'skills.id_technologie')
        ->where('skills.id_skill', '=', 1)
        ->get();
    return response()->json($users,200);
}

1 个答案:

答案 0 :(得分:0)

原件:

您确定输入了正确的列吗? 我在相片中看到的是id_skillsid_technologie

DB::table('employe')
->join('skills', 'skills.id_skills', '=', 'employe.id_skills')
->join('technologies', 'technologies.id_technologie', '=', 'skills.id_technologie')
->where('skills.id_skills', 1)
->get();

新:

你想在1行中获得2个结果。 并通过1 Query SQL。 您不知道有多少technologie一个skills相关。

我想解决的方法是首先选择skills

然后找到technologie,您可以将其命名为Name1Name2等。

请原谅我英语不好。