[英]How to retrieve data with 3 tables in mysql?
I am having 2 tables : 我有2张桌子:
1.internal_employee_master 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 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. 我正在使用UNION查询将两个表的数据都放入单个表中,并将此数据显示到html表中。
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. 我有一张带有
emp_id
和emp_type
列的表payslips
。
I am storing data into payslips
data like: 我将数据存储到
payslips
数据中,例如:
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
andemp_type
of both the tables in single columns each.如您在上表中看到的,我将两个表的
emp_id
和emp_type
都存储在单个列中。Now, i dont undestand how to split data of internal employee and external employee from
pay_slip
table and show data in html table.现在,我不理解如何从
pay_slip
表中拆分内部员工和外部员工的数据,并在html表中显示数据。Currently, i am writing below sql joins to get
employee_names
of internal and external employee tables but it doesnt work for me.目前,我写下面SQL连接获取
employee_names
内部和外部的员工表,但它不为我工作。
$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. 请帮助我加入查询以获取有关
emp_type
表单pay_slip
表的name
和employee_name
。
How about using UNION again? 再次使用UNION怎么样?
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 您可以在这里看到它的作用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. 例如,表之间不规则的列名(名称与employee_name),你已经错过了
is_deleted
从你的例子架构列,并且您已经资本化,并在非资本值emp_type
列这是令人困惑的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.