|
|
||||||
|
||||||
| Index Link To US Private Messages Archive FAQ RSS | ||||||
| Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here! |
Share Thread: & Tags
|
||||
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
I need some help with this.
I'm using three different mysql tables. 1st - Accounts. This holds the username needed for this script 2nd - Downloads. Contains the filename, userid, download count, etc 3rd - Rating. Contains all the ratings for every file. Each Rating needs to be grouped by and to average the data together. Then I need to be able to order all this data (user choice). Any ideas on how to do this? This is about my closest idea on how it might be done, but oviously no. $od - Contains the string to order by. Example: $od = "filename" $dir2 - Contains "DESC" or is left blank. $filetype = 0 - 5. 0 will list all the files. $sql = "SELECT * FROM Accounts, Rating, Downloads WHERE Accounts.id = Downloads.userid, SELECT Rating.fileid, AVG(Rating.rating) GROUP BY Rating.fileid"; if($filetype == 0) $sql .= "ORDER BY $od $dir2"; else $sql .= "AND Downloads.type = '$filetype' ORDER BY $od $dir2"; $result=mysql_query($sql); So anyone got any clues? |
|
|||
|
I'll try your script shortly.
I'm trying to get the output from all three of those tables. I need the output of the userids, the average rating for that file, and the username for who made the file. Imagine a page where you see a list of files. Filename, Username, Rating, Downloads, Last Updated. The Filename, userid(not username) downloads, and last updated can all be retrieved with the Download MYSQL table. The userid has to search the MYSQL table "Accounts" to get the proper username. The Rating has to search the MYSQL table "Rating" to average out all the ratings for that one file. Now when it's listing all this data you can click on the text "Filename","Username","Rating","Downloads","La st Updated" to choose the ordering. So to do this it needs to search through three mysql tables at once. Sometimes forums have it set up like this when searching through the "Member list" |
|
||||
|
Basically, the results set needs to come from the filename table then, because you want one response for each item in that table. Still, the most complex thing you need to get is the Rating of each file, but the core query must be done on the file table to allow the usernames to be retrieved more efficiently. Our starting point still needs to be ensuring that the query returns accurate ratings for each item. After you try the above query, try this one:
SELECT d.filename, AVG(r.rating) rating, d.fileid FROM Downloads d LEFT JOIN Ratings r ON d.fileid = r.fileid ORDER BY rating DESC LIMIT 100; See if you get the same results. You can test the accuracy of this query by spot checking a few of the results with the following query: SELECT AVG(rating) FROM Ratings WHERE fileid = whatever; replacing whatever with the fileid from the previous query. This is something you would do manually, to make sure the original query is correct. The result of this query should match the rating in the rating column for the specified file from the previous query. Once we are sure this query returns accurate data, we can easily incorporate retrieval of the user names.
__________________
The best way to learn anything, is to question everything. Last edited by wige; 06-10-2008 at 04:45 PM. |
|
|||
|
Just getting errors.
Some more info for the actual mysql tables. Table - Accounts id username password privilagee avatar signature rank date flags lastlogin Table - Downloads id name userid description link date type downloads info lastupdate Table - Rating id rating memidfileid Last edited by KingOfHeart; 06-10-2008 at 06:53 PM. |
|
|||
|
Hmm, don't see an edit button.
$sql = "SELECT Accounts.username, Accounts.id, Downloads.name, Downloads.userid, Downloads.lastupdate, Rating.fileid, Rating.rating FROM Accounts, Downloads, Rating WHERE Accounts.id = Downloads.userid"; This script gets the data from all three tables but can't group by or average out the ratings. I had another thought. Would it be good to just stick in a rating field in the Downloads, and updated this info when averaging the data. So it would go 1. Average all the ratings by reading the Rating table. 2. Use that info and Update the Rating field in the Downloads table. 3. Now just read the Downloads table info for easy ORDER BY results. Good idea or not? |
|
||||
|
Sticking an additinal stats field or creating a table for this would be recommended for system speed.
You should really consider creating special tables for these stats (i.e. ratings) and have the stats updated for a user when another user votes. Developing like this would solve future server load issues. Realtime calculations are ok if you have hundreds or a couple of thousands of users but this could kill the server when your sites becomes crowded with tens of thousands of users and hundreds of ratings or other data for each one. |
|
|||
|
Not worried about that script anymore. Now I'm working on a script to average all the ratings and then update that info in the Downloads table.
Code:
function GetRating()
{
$ratings = mysql_query("SELECT fileid, AVG(rating) FROM Rating GROUP BY fileid");
$downloads = mysql_query("SELECT * FROM Downloads");
$n = 1;
while($row = mysql_fetch_array($downloads)) {
$raterow = mysql_fetch_array($ratings);
$rate = ($raterow['AVG(rating)'] == false ? -1 : $raterow['AVG(rating)']);
echo "row " . $rate;
if($rate != -1)
{
mysql_query("UPDATE Downloads SET rating='{$rate}' WHERE id='{$raterow['fileid']}'");
}
else
{
mysql_query("UPDATE Downloads SET rating='0' WHERE id='$n'");
}
$n++;
}
}
So I hope this script is a lot easier to help me with. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| MYSQL - ORDER BY x DESC | kruser | Database Discussion Forum | 6 | 08-30-2007 07:22 AM |
| MySQL Multiple Select Question | ackerley1 | Database Discussion Forum | 24 | 08-28-2007 11:41 PM |
| Multi-select Dropdown Box | EmmaGale | Web Programming Discussion Forum | 0 | 09-29-2005 09:42 AM |
| Multi Table Update | fastedge | Database Discussion Forum | 2 | 04-27-2004 11:13 AM |
| PHP/MySQL - Populating the Select option of an HTML Form | johnwrightson | Web Programming Discussion Forum | 3 | 04-05-2004 02:27 AM |
|
WebProWorld |
Advertise |
Contact Us |
About |
Forum Rules |
MVP's |
Archive |
Newsletter Archive |
Top |
WebProNews
WebProWorld is an iEntry, Inc. ® site - © 2009 All Rights Reserved Privacy Policy and Legal iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509 |