php - outer - where like laravel
MYSQL Unirse a mĂșltiples columnas de la misma mesa (2)
Estoy tratando de obtener el nombre de habilidad para skill1
, skill2
, y skill3
de la tabla2 usando Join.
Funciona bien cuando trato de obtener skill1
solo. Pero, 1066 Error no exclusivo de tabla / alias aparece cuando trato de obtener detalles para la siguiente columna.
Tabla 1 (tabla de usuario)
======================================
ID Name skill skill2 skill3
======================================
1 Ed 1 4 3
--------------------------------------
Tabla 2 (detalles de la habilidad)
=========================
ID Skill Name
=========================
1 php
2 html
3 css
4 mysql
-------------------------
Esto es lo que espero obtener:
[name] => ''Ed''
[skill1] => ''php''
[skill2] => ''mysql''
[skill3] => ''css''
Aquí está mi código, estoy usando laravel:
DB::table(''table1'')
->join(''table2'', function($join)
{
$join->on(''table1.skill1'', ''='', ''table2.id'');
})
->join(''table2'', function($join)
{
$join->on(''table1.skill2'', ''='', ''table2.id'');
})
->join(''table2'', function($join)
{
$join->on(''table1.skill3'', ''='', ''table2.id'');
})
->get();
Mismo resultado que la consulta @erickmcarvalho
SELECT Table1.usrname,
(SELECT Table2.skillname FROM Table2 WHERE Table1.skill1 = Table2.Id) As skill1,
(SELECT Table2.skillname FROM Table2 WHERE Table1.skill2 = Table2.Id) As skill2,
(SELECT Table2.skillname FROM Table2 WHERE Table1.skill3 = Table2.Id) As skill3
FROM Table1
Aún causa 4 consultas, sería mejor reestructurar tablas
Prueba esta consulta:
SELECT U.Name AS Name, S1.Skill Name AS Skill1, S2.Skill Name AS Skill2, S3.Skill Name AS Skill3
FROM table1 U
JOIN table2 S1 ON (S1.Id = U.skill1)
JOIN table2 S2 ON (S2.Id = U.skill2)
JOIN table2 S3 ON (S3.Id = U.skill3)