Ud over at have ReferenceType-kolonnen i ref_types-tabellen, har du også brug for en Reference_ID-kolonne, der refererer til det faktiske ID i den tilsvarende tabel som en fremmednøgle .
//ref_types table
ID Article_ID ReferenceType Reference_ID
1 1 book 1
2 1 article 1
3 1 article 2
4 1 book 2
Så kan du undgå en WHILE-løkke og lade MySQL gøre arbejdet for dig med JOINs:
SELECT CONCAT('record ', rt.ID, ': ',
COALESCE(ar.Article_Title, tr.Thesis_Title, br.Book_Title))
FROM ref_types rt
LEFT JOIN article_refs ar
ON rt.ReferenceType = 'article' AND ar.ID = rt.Reference_ID
LEFT JOIN book_refs br
ON rt.ReferenceType = 'book' AND br.ID = rt.Reference_ID
LEFT JOIN thesis_refs tr
ON rt.ReferenceType = 'thesis' AND tr.ID = rt.Reference_ID
Giver resultatet:
record 1: book1
record 2: article1
record 3: article2
record 4: book2