Hey phpbuilder.com,
this may be a relative easy problem to solve but its getting me a massive headache!
First off i will tell you what my database looks like, i have three tables:
Table name: auto (Translated for you: car)
| a_id a_type g_id
| 1 | Porsche 911 | 3 |
| 2 | Honda civic | 2 |
Table name: gebruikers (Translated for you: users)
| g_id a_username g_wachtwoord (translated: password)
| 1 | administrator | poodle |
| 2 | user1 | pug |
| 3 | user2 | coffee |
Table 3: rating
| a_id g_id score
| 1 | 2 | 5 |
| 1 | 1 | 3 |
OK so here's my query i use to get all the data out:
$query = "SELECT * FROM auto
LEFT JOIN gebruikers ON auto.g_id = gebruikers.g_id
LEFT JOIN rating ON auto.a_id = rating.a_id
ORDER BY a_type";
And this results in:
| a_id a_type score g_id
| 1 | Porsche 911 | 5 | 3
| 1 | Porsche 911 | 3 | 3
| NULL| Honda civic | NULL | 2
My questions:
How can i make it so that if a car is rated twice that i only get 1 row with the
score averaged out? like this: 1 Porsche 911 4 3. i tried using AVG() but i get
a syntax error each time i try.
Why is the a_id on non-rated cars NULL? how can i fix this?
Thanks in advance!
Xafro