Hi,
you can use IF statements in MySQL queries.
Example:
SELECT DISTINCT table1.id, table1.playerType, table1.mydate,
IF (table1.playerType='0',table2.myName, table3.myName)
FROM table1,table2,table3
WHERE table1.mydate='$mydate'
AND IF (table1.playerType='0',table1.myName = table2.id,table1.myName = table3.id)
Shorter:
SELECT DISTINCT t1.id, t1.playerType, t1.mydate,
IF (t1.playerType='0',t2.myName, t3.myName)
FROM table1 t1,table2 t2,table3 t3
WHERE t1.mydate='$mydate'
AND IF (t1.playerType='0',t1.myName = t2.id,t1.myName = t3.id)
Something like that worked for me. But I had to use DISTINCT. The values that were returned seemed to be correct.
Hope that solves the problem or at least gives you a direction to search in.
This query was a bit time expensive so your php solution might be faster.
Depending on the MySQL version using UNION might be another way. But I'm not sure.
A very important task is to create indexes on the fields at least used in the where clause where applicable (it's not in every case as easy as creating an index on all fields).
You can use
EXPLAIN sql_query
to see which indexes MySQL uses.
Thomas