I have tables:
events
-----+-------------------+
| id | eventName |
-----+-------------------+
| 1 | Bowling |
| 2 | Quiz |
| 3 | Darts |
| 4 | Pool |
| 5 | Treasure Hunt |
| 6 | Snooker |
-----+-------------------+
competitors
------+------------------+
| id | competitorName |
------+------------------+
| 1 | Microsoft |
| 2 | BBC |
| 3 | Facebook |
------+------------------+
results
------+---------------+---------+-------------+
| id | competitorId | eventId | eventScore |
------+---------------+---------+-------------+
| 1 | 1 | 1 | 12 |
| 2 | 1 | 2 | 11 |
| 3 | 1 | 3 | 23 |
| 4 | 2 | 1 | 66 |
| 5 | 2 | 2 | 12 |
| 6 | 2 | 3 | 11 |
| 7 | 2 | 4 | 23 |
| 8 | 2 | 5 | 66 |
| 3 | 2 | 6 | 23 |
| 4 | 3 | 1 | 66 |
| 5 | 3 | 2 | 12 |
| 6 | 3 | 3 | 11 |
| 7 | 3 | 4 | 23 |
| 8 | 3 | 6 | 66 |
------+---------------+---------+-------------+
From which I want to achieve output like this:
--------------+---------+---------+--------+------+--------------+----------
| competitor | Bowling | Quiz | Darts |Pool |Treasure Hunt | Snooker |
--------------+---------+---------+--------+------+--------------+----------
| Microsoft | 12 | 11 | 23 | 0 | 0 | 0 |
| BBC | 66 | 12 | 11 | 23 | 66 | 23 |
| Facebook | 66 | 12 | 11 | 23 | 0 | 66 |
--------------+---------+---------+--------+------+--------------+----------
I have tried all sorts of joins and nested php but I just can’t crack it. I’m not even sure if it’s possible. My SQL knowledge is not great, I’ve tried some tutorials but they don’t cover this kind of query.
You can create your query by php like this
Then, to get row dynamically