Web Hosting Talk







View Full Version : SQL Query


GregVernon
10-07-2008, 11:22 PM
Hi.

I need to write an SQL statement which orders ONE table by an average 'star' rating in a separate table. Where would I even start with this?

I need all the data from the first table 'companies'. The primary key is "id".

The table, 'review' has a structure like this:
r_id, primary key, auto-increment
parent_id, used to identify reviews to companies
stars, what I need to average and order by

vibrokatana
10-07-2008, 11:58 PM
Hi.

I need to write an SQL statement which orders ONE table by an average 'star' rating in a separate table. Where would I even start with this?

I need all the data from the first table 'companies'. The primary key is "id".

The table, 'review' has a structure like this:
r_id, primary key, auto-increment
parent_id, used to identify reviews to companies
stars, what I need to average and order by

SELECT *, AVG(stars) AS rating FROM companies LEFT JOIN review on companies.id = review.parent_id GROUP BY companies.id ORDER BY rating DESC;

Donno if that is what you are looking for and I haven't tried to run it.

GregVernon
10-08-2008, 12:20 AM
SELECT *, AVG(stars) AS rating FROM companies LEFT JOIN review on companies.id = review.parent_id GROUP BY companies.id ORDER BY rating DESC;

Donno if that is what you are looking for and I haven't tried to run it.

Yes this works! Thanks!