MYSQL Join multiple column from same table

Ahmad Hafiz

I'm trying to get skill name for skill1,skill2, & skill3 from the table2 by using Join.

It works fine when Im trying to get skill1 alone. But, 1066 Not unique table/alias error comes out when I try to get details for the next column.


Table 1 (User table)

======================================
ID  Name       skill   skill2   skill3
======================================
1   Ed           1       4       3    
--------------------------------------

Table 2 (Skill details)

=========================
ID  Skill Name
=========================
1   php
2   html
3   css
4   mysql
-------------------------

This is what I expect to get:

[name]    => 'Ed'
[skill1]  => 'php'
[skill2]  => 'mysql'
[skill3]  => 'css'

Here's my code, I'm using 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();
erickmcarvalho

Try this query:

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL Join multiple column from same table

From Dev

Pulling multiple rows from the same table from a JOIN table in MySQL

From Dev

mysql update table column with multiple values from same table

From Dev

MYSQL Join Multiple times on same table

From Dev

Join on multiple ids from the same table

From Dev

Inner join multiple times on same table on same column

From Dev

Join two fields from same table in mysql

From Dev

MySql LEFT join multiple table same with same id or name

From Dev

MySql LEFT join multiple table same with same id or name

From Dev

Multiple join on same table

From Dev

How can I left join multiple data from the same mySQL table?

From Dev

join same table in mysql

From Dev

Multiple Column results from the same table

From Dev

select sum() from multiple table with same column

From Dev

Join all multiple columns to the same column of another table

From Dev

MySQL Join Table which have had same column name

From Dev

MYSQL Query with Multiple Selects from Same Table

From Dev

MySQL - Group by multiple columns from same table

From Dev

MySQL left outer join the same table multiple times?

From Dev

MySQL left outer join the same table multiple times?

From Dev

MySQL: combining multiple WHERE clauses from same table with diff column names

From Dev

Join multiple values from one column, selected from another table

From Dev

Select/Join multiple fields from different tables with same column name

From Dev

Select/Join multiple fields from different tables with same column name

From Dev

Using join to return multiple data cells from the same column

From Dev

Update multiple column value from one column of the same table

From Dev

Update multiple column value from one column of the same table

From Dev

SQL Alchemy Join Multiple Columns from same table

From Dev

Referencing records from the same table multiple times in a join query?

Related Related

  1. 1

    MYSQL Join multiple column from same table

  2. 2

    Pulling multiple rows from the same table from a JOIN table in MySQL

  3. 3

    mysql update table column with multiple values from same table

  4. 4

    MYSQL Join Multiple times on same table

  5. 5

    Join on multiple ids from the same table

  6. 6

    Inner join multiple times on same table on same column

  7. 7

    Join two fields from same table in mysql

  8. 8

    MySql LEFT join multiple table same with same id or name

  9. 9

    MySql LEFT join multiple table same with same id or name

  10. 10

    Multiple join on same table

  11. 11

    How can I left join multiple data from the same mySQL table?

  12. 12

    join same table in mysql

  13. 13

    Multiple Column results from the same table

  14. 14

    select sum() from multiple table with same column

  15. 15

    Join all multiple columns to the same column of another table

  16. 16

    MySQL Join Table which have had same column name

  17. 17

    MYSQL Query with Multiple Selects from Same Table

  18. 18

    MySQL - Group by multiple columns from same table

  19. 19

    MySQL left outer join the same table multiple times?

  20. 20

    MySQL left outer join the same table multiple times?

  21. 21

    MySQL: combining multiple WHERE clauses from same table with diff column names

  22. 22

    Join multiple values from one column, selected from another table

  23. 23

    Select/Join multiple fields from different tables with same column name

  24. 24

    Select/Join multiple fields from different tables with same column name

  25. 25

    Using join to return multiple data cells from the same column

  26. 26

    Update multiple column value from one column of the same table

  27. 27

    Update multiple column value from one column of the same table

  28. 28

    SQL Alchemy Join Multiple Columns from same table

  29. 29

    Referencing records from the same table multiple times in a join query?

HotTag

Archive