(ID) [category] {name}
(1) [1] {Tom}
(2) [1] {Jane}
(3) [1] {Kate}
(4) [4] {John}
(5) [2] {Ryan}
(6) [3] {Ted}
I have the above data in myTable of MySQL.
The table has three fields named (ID), [catagory] and {name}.
It has 6 records at the moment.
record (1)'s catagory is "1".
record (2)'s catagory is also "1".
record (3)'s catagory is also "1".
record (4)'s catagory is "4".
record (5)'s catagory is "2".(please notice the category value "2" is the name of record (2) which has the category value "1")
record (6)'s catagory is "3".(please notice the category value "3" is the name of record (3) which has the category value "1")
Let's suppose I have to output all records which have direct or indirect relation with catagory "1".
record (1), record (2), and record (3) have direct relation with catagory "1" because they have the value "1" in the catagory field.
record(4) has no relation with the catagory "1".
record(5) has indirect relation with the catagory "1" because its value in catagory is "2" and the record (2) has the value "1" in the catagory field.
record(6) has indirect relation with the catagory "1" because its value in catagory is "3" and the record (3) has the value "1" in the catagory field.
If I use the following code, I can produce all records which is related with the category value {1}, i.e (1),(2),(3),(5),(6), here.
select t1.ID, t1.category,t1.name
from myTable t1
join myTable t2
on t1.category = t2.ID
join myTable t3
on t2.category = t3.ID
where t1.category = 1
or t2.category = 1
or t3.category = 1
With the above code, the following result would be produced.
(ID) [category] {name}
(1) [1] {Tom}
(2) [1] {Jane}
(3) [1] {Kate}
(5) [2] {Ryan}
(6) [3] {Ted}
Now I'll tell you what I want.
my taget output is the following.
(ID) [category] {name}
(1) [1] {Tom}
(2) [1] {Tom}
(3) [1] {Tom}
(5) [2] {Jane}
(6) [3] {Kate}
record (1)'s category value [1] is the same name of the record (1) and the record (1) has the name value {Tom}.
record (2)'s category value [1] is the same name of the record (1) and the record (1) has the name value {Tom}.
record (3)'s category value [1] is the same name of the record (1) and the record (1) has the name value {Tom}.
record (5)'s category value [2] is the same name of the record (2) and the record (2) has the name value {Jane}.
record (6)'s category value [3] is the same name of the record (3) and the record (3) has the name value {Kate}.
How can I produce my target output?
Thanks in Advance