Home > Mobile >  SQL Multiple join from 2 table to 1
SQL Multiple join from 2 table to 1

Time:08-02

I want to join from 2 table to 1. I have tried it multiple times but I can't figure out the correct code. The name 'Edvard Supica' which is the full_name from suser table I want to be on the place of User ID and in 1 record.

how it looks like now'

SELECT w.*, m.vin AS 'workbook_vin'
FROM workbook w
LEFT JOIN machines m ON m.id = w.machine_id
            
UNION

SELECT w.*, s.full_name AS 'user_full_name'
FROM workbook w
LEFT JOIN suser s ON s.id = w.user_id

CodePudding user response:

If you're joining both machines and susers on workbook, you can do both JOINs in the same query without needing to use UNION.

SELECT
    w.*,
    m.vin AS 'workbook_vin',
    s.full_name AS 'user_full_name'
FROM
    workbook w
    LEFT JOIN machines m ON m.id = w.machine_id
    LEFT JOIN suser s ON s.id = w.user_id
  • Related