Reputation: 41
I have a table like this:
| id | parentID | columnName |
|----|----------|-------------------|
| 1 | 2 | John |
| 2 | 0 | Task name |
| 3 | 4 | John |
| 4 | 0 | Task another name |
I want to get John task, but also with its name so I'm wondering if it is any way to do it in single MySQL query?
SELECT id, parentID, person FROM table WHERE columnName = "John"
will result in user task, but I want to know how this task is named.
Upvotes: 0
Views: 53
Reputation: 222402
You seem to be looking for a self-join:
select
t.id,
t.parentid,
t.columnname person,
t1.columnname task
from mytable t
left join mytable t1 on t1.id = t.parentid
where t.person = 'John'
Upvotes: 1