|
No!!!
It's not possible, at least easily, to do what you want using SQL. You could come up with a hack to do it, probably using a cursor, and procedural SQL code ... but there's a saying that if you can't write a simple SQL query to get what you need, something is wrong with the query or the schema.
In your case, it's with the schema. Don't go adding columns on the fly like that!! Seriously, there are plenty of reasons why this is bad. You can run into duplicate or illegal column names, you're limited to a certain number of columns per table ( in the thousands, though ), you can only store a certain number of bytes per row ... every new column you add slows down the responsiveness of the table.
And, most basic of all the reasons, it's just not relational design. Instead, you could use a table with a column called bandname, another for the name fo the user, and a third for the rating. Add a row, instead of a column, to store more data. It sounds like a meaningless difference, but this way is what databases are good at, so can do quickly, easily, it gives you options to enforce referential integrity, and your query becomes amazingly easy.
If you do it this way, you can get what you want with a SQL query like Select Top 100 Sum(Rating), Avg(Rating) From bandname Where band="xxx" Group By band Order By Sum(Rating) Desc. That would be the SQL Server syntax; I think in MySQL you use a where clause instead of a top x clause.
Besides being simpler, this is also going to do the job in less time using less memory on your server. The database engine is highly optimized for this kind of stuff, and does a much better job than PHP loops.
|