Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually facing prob in join...
"agents_details" Table structure: "Agent_Id"(PK),"Agent_Emp_Id"...

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
Posted
Updated 25-Mar-11 20:55pm
v2
Comments
Sandeep Mewara 26-Mar-11 2:31am    
Care to elaborate that what is the issue?

Until you don't frame your question properly, how are we suppose to help you out?
Om Prakash Pant 26-Mar-11 2:47am    
can you provide the database schema for both the tables and the relation between them?

Your first query would be like
SQL
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.
 
Share this answer
 
Comments
DEB4u 26-Mar-11 3:02am    
i need to the result of those 2 query in 1 table...
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.
 
Share this answer
 
Comments
DEB4u 26-Mar-11 4:40am    
thanks a lot....
Wendelius 26-Mar-11 4:59am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900