加入mysql的问题 [英] problem in Join in mysql

查看:76
本文介绍了加入mysql的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上在连接中面临问题...
"agents_details"表结构:"Agent_Id"(PK),"Agent_Emp_Id" ...

来自"employee_details.Employee_Id"的外键"Agent_Emp_Id"

"employee_details"结构:"Employee_Id"(PK),"Emp_Name","Emp_Mname" ....

"employee_status"结构:"Emp_Id"(pk),"Emp_Acc_Stat" ....

来自"employee_details.Employee_Id"的外键"Emp_Id"

现在需要获取其employee_status.Emp_Acc_Stat ==''OPEN''
的座席名称(即employee_details.Emp_Name)和agent_Id(即agents_details.Agent_Id)
我分两步完成了它们.在我的第一个查询中,我已获取支持该条件的代理ID,并在下一步中获取了满足条件的代理的名称...

现在需要合并并需要一张桌子....

:

"select agents_details.Agent_Employee_Id,agents_details.Agent_Id from agents_details where Agent_Employee_Id=any(select Emp_Id from employee_status where Emp_acc_Stat=''OPEN'') "


另一个查询:

"select Employee_Id,Emp_Name,Emp_Mname,Emp_Lname from employee_details where Employee_Id=any(select agents_details.Agent_Employee_Id from agents_details where Agent_Employee_Id=any(select Emp_Id from employee_status where Emp_acc_Stat=''OPEN'') ) "




1个表中需要Agent_Id,Emp_Name

请帮助我是sql中的新手

解决方案

您的第一个查询就像
INNER JOIN employee_status ON agents_details.agent_employee_id = employee_status.emp_id 其中 Emp_acc_Stat = ' OPEN'



它将使用agent_employee_id作为Primary Key联接两个表,而employee_status中的emp_id将成为Foreign Key.

用类似的方法尝试第二个查询.而不是使用两个where子句,使用ANDOR或任何可用的联合运算符将它们组合为一个where子句.本练习将帮助您了解基本的查询结构.


使用内联视图的一种方法.如果集合之间的键是emplyoee_id,则查询可能类似于:

SELECT * 
FROM (select agents_details.Agent_Employee_Id,agents_details.Agent_Id 
      from agents_details 
      where Agent_Employee_Id=any(select Emp_Id 
                                  from employee_status 
                                  where Emp_acc_Stat=''OPEN'')) a,
     (select Employee_Id,Emp_Name,Emp_Mname,Emp_Lname 
      from employee_details 
      where Employee_Id=any(select agents_details.Agent_Employee_Id 
                            from agents_details
                            where Agent_Employee_Id=any(select Emp_Id 
                                                        from employee_status 
                                                        where Emp_acc_Stat=''OPEN'') )) b
WHERE a.Agent_Employee_Id = b.Employee_Id


如果仅要将两个结果添加到单个结果中,请使用 UNION [
Foreign key "Agent_Emp_Id" from "employee_details.Employee_Id"

"employee_details" structure: "Employee_Id"(PK),"Emp_Name","Emp_Mname"....

"employee_status" structure: "Emp_Id"(pk),"Emp_Acc_Stat"....

Foreign key "Emp_Id" from "employee_details.Employee_Id"

now it''s required to to fetch the agent name(i.e. employee_details.Emp_Name) and agent_Id(i.e.agents_details.Agent_Id) whose employee_status.Emp_Acc_Stat==''OPEN''

i''ve done them in 2 steps..i.e. in my 1st query i''ve fetched agent id which supports the condition and in next step i''ve fetched the name of agents which satisfy the cond...

now need to combine and require in 1 table....

:

"select agents_details.Agent_Employee_Id,agents_details.Agent_Id from agents_details where Agent_Employee_Id=any(select Emp_Id from employee_status where Emp_acc_Stat=''OPEN'') "


Another query:

"select Employee_Id,Emp_Name,Emp_Mname,Emp_Lname from employee_details where Employee_Id=any(select agents_details.Agent_Employee_Id from agents_details where Agent_Employee_Id=any(select Emp_Id from employee_status where Emp_acc_Stat=''OPEN'') ) "




need Agent_Id,Emp_Name in 1 table

kindly help i''m new in sql

Your first query would be like

select agents_details.Agent_Employee_Id,agents_details.Agent_Id from agents_details INNER JOIN employee_status ON agents_details.agent_employee_id=employee_status.emp_id where Emp_acc_Stat='OPEN'



It will join the two tables using the agent_employee_id as Primary Key and emp_id in employee_status would be the Foreign Key.

Similar way try your second query. Instead of using two where clauses combine them into one where clause using AND or OR or else any conjunction operators available. This exercise will help you understand basic query structure.


One way s to use inline view. If the key between the sets is the emplyoee_id then the query could look something like:

SELECT * 
FROM (select agents_details.Agent_Employee_Id,agents_details.Agent_Id 
      from agents_details 
      where Agent_Employee_Id=any(select Emp_Id 
                                  from employee_status 
                                  where Emp_acc_Stat=''OPEN'')) a,
     (select Employee_Id,Emp_Name,Emp_Mname,Emp_Lname 
      from employee_details 
      where Employee_Id=any(select agents_details.Agent_Employee_Id 
                            from agents_details
                            where Agent_Employee_Id=any(select Emp_Id 
                                                        from employee_status 
                                                        where Emp_acc_Stat=''OPEN'') )) b
WHERE a.Agent_Employee_Id = b.Employee_Id


If you want to just add the two results to a single results, use
UNION[^] but in that case you will need to ensure that the columns and their datatypes match.


这篇关于加入mysql的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆