For at få alle tabellers statistik, kan du bruge en UNION, med 2 eller flere valg, en for hver tabel:
( SELECT s.*
, table1.title AS name --or whatever field you want to show
FROM stats s
JOIN $tableName1 table1
ON s.id = table1.id
WHERE tableName = '$tableName1'
)
UNION ALL
( SELECT s.*
, table2.name AS name --or whatever field you want to show
FROM stats s
JOIN $tableName2 table2
ON s.id = table2.id
WHERE tableName = '$tableName2'
)
UNION ALL
( SELECT s.*
, table3.lastname AS name --or whatever field you want to show
FROM stats s
JOIN $tableName3 table3
ON s.id = table3.id
WHERE tableName = '$tableName3'
)
;
Bruger Winfreds idé med LEFT JOIN
s. Det giver forskellige resultater, f.eks. hvert felt fra de andre tabeller udlæses i sin egen kolonne (og mange NULL'er forekommer).
SELECT s.*
, table1.title --or whatever fields you want to show
, table2.name
, table3.lastname --etc
FROM stats s
LEFT JOIN $tableName1 table1
ON s.id = table1.id
AND s.tableName = '$tableName1'
LEFT JOIN $tableName2 table2
ON s.id = table2.id
AND s.tableName = '$tableName2'
LEFT JOIN $tableName3 table3
ON s.id = table3.id
AND s.tableName = '$tableName3'
--this is to ensure that omited tables statistics don't appear
WHERE s.tablename IN
( '$tableName1'
, '$tableName2'
, '$tableName3'
)
;