简体   繁体   中英

How to retrieve data with 3 tables in mysql?

I am having 2 tables :

1.internal_employee_master

  id    employee_name     unique_id
   1     Noah               ABCD
   2     Liam               ABCD
   3     William            ABCD
   4     Benjamin           ABCD
   5     Jacob              EFGH

2.external_employee_master

  id    name             unique_id
   1    Elijah             ABCD
   2    Ethan              ABCD
   3    Alexander          EFGH

I am using UNION query to get both tables data into single table and display this data into html table.

select id
     , employee_name
     , unique_id
  from internal_employee_master
 where unique_id = 'ABCD'
union
select id
     , employee_name
     , unique_id
  from external_employee_master
 where unique_id = 'ABCD'

I want to store payslips of both employees into single table. I have one table payslips with emp_id and emp_type columns.

I am storing data into payslips data like:

   id     pay_slip        emp_id  emp_type
   1   Noah_payslip.pdf    1     internal
   2   Liam_payslip.pdf    2     internal
   3   Lia_payslip.pdf     1     External

as you can see in above table i am storing emp_id and emp_type of both the tables in single columns each.

Now, i dont undestand how to split data of internal employee and external employee from pay_slip table and show data in html table.

Currently, i am writing below sql joins to get employee_names of internal and external employee tables but it doesnt work for me.

$id = $_GET['id];
SELECT ps.id,ps.pdf,ps.emp_id,ps.emp_type,external_employee.name as comemp,
internal_employee.comp_empl_name as comemp
FROM pay_slip as  ps 
INNER JOIN internal_employee_master as internal_employee ON internal_employee.comp_trad_id = ps.trade_id 
INNER JOIN external_employee_master as external_employee ON external_employee.trad_id = ps.trade_id
where ps.is_deleted = 1 AND ps.id = '".$id."'"

Please help me to join query to get name and employee_name with respect to emp_type form pay_slip table.

How about using UNION again?

SELECT 
    ps.id,
    ps.pdf,
    ps.emp_id,
    ps.emp_type,
    external_employee.name AS comemp,
    internal_employee.comp_empl_name AS comemp
FROM
    pay_slip AS ps
        INNER JOIN
    internal_employee_master AS internal_employee ON internal_employee.comp_trad_id = ps.trade_id
WHERE
    ps.is_deleted = 1 AND ps.id = '".$id."'
        AND ps.type = 'internal' 
UNION ALL 
SELECT 
    ps.id,
    ps.pdf,
    ps.emp_id,
    ps.emp_type,
    external_employee.name AS comemp,
    internal_employee.comp_empl_name AS comemp
FROM
    pay_slip AS ps
        INNER JOIN
    external_employee_master AS external_employee ON external_employee.trad_id = ps.trade_id
WHERE
    ps.is_deleted = 1 AND ps.id = '".$id."'
        AND ps.type = 'external'

You could try this

SELECT ps.id, ps.pay_slip, ps.emp_type, COALESCE(i.employee_name, e.name) AS name 
FROM payslips ps 
LEFT JOIN internal_employee_master i ON i.id = ps.emp_id AND ps.emp_type = 'internal'
LEFT JOIN external_employee_master e ON e.id = ps.emp_id AND ps.emp_type = 'External' 
AND ps.id = :ID

You can see this in action here http://sqlfiddle.com/#!9/53a195/7/0

I would mention that there are a number of issues in your included tables and queries. For example, irregular column names between tables (name vs. employee_name), you've missed the is_deleted column from your example schema, and you have capitalised and non-capitalised values in the emp_type column which is confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM