2

I have table which have id name and manager id

id  name  manager_id
1  myName  4
2  alex    3
3  brain   2
4  someone 1

Now i have to write the query which display the result like that

myname someone
alex brain
brain alex
someone myname

Means we have to display name and mnager Name according to the Id

1
  • Read about left join. (i wouldn't call it complex ...) Commented Aug 1, 2011 at 10:30

3 Answers 3

5

join the table with itself:

SELECT t1.name, t2.name FROM my_table AS t1 
    LEFT JOIN my_table AS t2 ON( t1.manager_id = t2.id )
Sign up to request clarification or add additional context in comments.

Comments

4

Does this return what youre after?

SELECT
  t1.name AS Name,
  t2.name AS manager
FROM tab t1
  JOIN tab t2 ON(t1.manager_id = t2.id)

Comments

2
SELECT
  employee.name,
  manager.name
FROM
  tablename employee
INNER JOIN
  tablename manager
ON
  employee.manager_id = manager.id

Replace tablename with the actual table name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.