I already addressed something of this nature using Stored Procedures : Find highest level of a hierarchical field: with vs without CTEs (Oct 24, 2011)
If you look in my post, you could use the GetAncestry and GetFamilyTree functions as a model for traversing the tree from any given point.
UPDATE 2012-12-11 12:11 EDT
I looked back at my code from my post. I wrote up the Stored Function for you:
DELIMITER $$
DROP FUNCTION IF EXISTS `cte_test`.`GetFamilyTree` $$
CREATE FUNCTION `cte_test`.`GetFamilyTree`(GivenName varchar(64))
RETURNS varchar(1024) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE rv,q,queue,queue_children,queue_names VARCHAR(1024);
DECLARE queue_length,pos INT;
DECLARE GivenSSN,front_ssn VARCHAR(64);
SET rv = '';
SELECT SSN INTO GivenSSN
FROM Employee
WHERE name = GivenName
AND Designation <> 'OWNER';
IF ISNULL(GivenSSN) THEN
RETURN ev;
END IF;
SET queue = GivenSSN;
SET queue_length = 1;
WHILE queue_length > 0 DO
IF queue_length = 1 THEN
SET front_ssn = queue;
SET queue = '';
ELSE
SET pos = LOCATE(',',queue);
SET front_ssn = LEFT(queue,pos - 1);
SET q = SUBSTR(queue,pos + 1);
SET queue = q;
END IF;
SET queue_length = queue_length - 1;
SELECT IFNULL(qc,'') INTO queue_children
FROM
(
SELECT GROUP_CONCAT(SSN) qc FROM Employee
WHERE MSSN = front_ssn AND Designation <> 'OWNER'
) A;
SELECT IFNULL(qc,'') INTO queue_names
FROM
(
SELECT GROUP_CONCAT(name) qc FROM Employee
WHERE MSSN = front_ssn AND Designation <> 'OWNER'
) A;
IF LENGTH(queue_children) = 0 THEN
IF LENGTH(queue) = 0 THEN
SET queue_length = 0;
END IF;
ELSE
IF LENGTH(rv) = 0 THEN
SET rv = queue_names;
ELSE
SET rv = CONCAT(rv,',',queue_names);
END IF;
IF LENGTH(queue) = 0 THEN
SET queue = queue_children;
ELSE
SET queue = CONCAT(queue,',',queue_children);
END IF;
SET queue_length = LENGTH(queue) - LENGTH(REPLACE(queue,',','')) + 1;
END IF;
END WHILE;
RETURN rv;
END $$
It actually works. Here is a sample:
mysql> SELECT name,GetFamilyTree(name) FamilyTree
-> FROM Employee WHERE Designation <> 'OWNER';
+------+-----------------------+
| name | FamilyTree |
+------+-----------------------+
| A | B,F,C,H,L,I,K,D,E,G,J |
| G | |
| D | |
| E | |
| B | C,H,L,I,D,E,G,J |
| F | K |
| C | D,E |
| H | G |
| L | |
| I | J |
| K | |
| J | |
+------+-----------------------+
12 rows in set (0.36 sec)
mysql>
There is only one catch. I added one extra row for the owner
- The owner has SSN 0
- The owner is his own boss with MSSN 0
Here is the data
mysql> select * from Employee;
+-----+------+-------------+------+
| SSN | Name | Designation | MSSN |
+-----+------+-------------+------+
| 0 | A | OWNER | 0 |
| 1 | A | BOSS | 0 |
| 10 | G | WORKER | 5 |
| 11 | D | WORKER | 4 |
| 12 | E | WORKER | 4 |
| 2 | B | BOSS | 1 |
| 3 | F | BOSS | 1 |
| 4 | C | BOSS | 2 |
| 5 | H | BOSS | 2 |
| 6 | L | WORKER | 2 |
| 7 | I | BOSS | 2 |
| 8 | K | WORKER | 3 |
| 9 | J | WORKER | 7 |
+-----+------+-------------+------+
13 rows in set (0.00 sec)
mysql>
A good starting place is to research database normalization. It explains some concepts on how to best structure tables.
More specifically, I'd name the tables "Song" (singular is often preferred) and "Artist" (probably a more central concept than a twitter account). A song would then have an ArtistID which refers to the Artist table. To get the twitter account for an artist you only need to look at the Artist table, you don't need to look at the Song table at all, e.g.:
select TwitterHandle
from Artist
where ArtistName='Beatle';
Best Answer
Procedure code
Test code
Test output