| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| ccTLD Specialist Join Date: Jan 2006
Posts: 802
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 25np$ for a simple SQL query I have a MySQL table with following structure: record_id - prod_name - prod_id - rating record_id is the only unique field. I can have multiple ratings for the same prod_id. I need one query (may be nested) that retreives all the fields of the record that has highest rating for each prod_id. For example if i have the following in the table: record_id - prod_name - prod_id - rating 1 aaa 10 6 2 bbb 11 7 3 aaa 10 9 4 bbb 11 5 5 ccc 9 3 The result of query should be 3 aaa 10 9 2 bbb 11 7 5 ccc 9 3
__________________ All offers from me are valid for 24 Hrs unless otherwise noted. |
| |
| | THREAD STARTER #4 (permalink) |
| ccTLD Specialist Join Date: Jan 2006
Posts: 802
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | JIM: I have around 100 prod_ids. For every homepage load I need to execute 100 queries. Its not a good idea right. Always better to minimise queries. Noobie: I also need the other parameters. prod_id is in group by so we can get its value. But I need the other fields as well record-id & prod_name. If i try the same query with these fields it doesnt work. anyway, thanks for your effort.
__________________ All offers from me are valid for 24 Hrs unless otherwise noted. |
| |
| | #5 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,963
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Using a subquery to determine the max rating for each record... PHP Code: ????: NamePros.com http://www.namepros.com/programming/184138-25np-for-a-simple-sql-query.html Also, please note that you need at least mySQL 4.0 (?) to do subqueries...
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists |
| |
| | #6 (permalink) | ||||
| Senior Member Join Date: Jan 2006
Posts: 1,127
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
For versions below 4.1 there is a workaround: ????: NamePros.com http://www.namepros.com/showthread.php?t=184138 http://dev.mysql.com/doc/refman/4.1/...ubqueries.html
__________________ | ||||
| |
| | THREAD STARTER #7 (permalink) | ||||
| ccTLD Specialist Join Date: Jan 2006
Posts: 802
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Hi dude, thanks. It works fine. 25np$ on the way!
__________________ All offers from me are valid for 24 Hrs unless otherwise noted. | ||||
| |
| | #8 (permalink) |
| Domains my Dominion Join Date: Aug 2005 Location: Web 1.0
Posts: 9,963
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Thanks for the NP$, glad it helps
__________________ NameNewsletter.com - free lists of available domain names ZoneFiles.net (beta) - ccTLD and gTLD droplists |
| |