NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page 25np$ for a simple SQL query

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 04-05-2006, 01:02 PM THREAD STARTER               #1 (permalink)
ccTLD Specialist
 
Smooth's Avatar
Join Date: Jan 2006
Posts: 802
Smooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of light
 


Cystic Fibrosis Cystic Fibrosis Cystic Fibrosis

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.
Smooth is offline  
Old 04-05-2006, 01:15 PM   #2 (permalink)
NamePros Regular
 
Jim_'s Avatar
Join Date: Aug 2005
Location: NY, USA
Posts: 608
Jim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to beholdJim_ is a splendid one to behold
 


Save The Children
Code:
SELECT * FROM `table_name` WHERE `prod_id` = # ORDER BY `rating` DESC LIMIT 0,1
Why not do that for each prod_id?
__________________
ask me about the internet
Jim_ is offline  
Old 04-05-2006, 01:26 PM   #3 (permalink)
NamePros Regular
 
Noobie's Avatar
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
Noobie is on a distinguished road
 



try this
Code:
SELECT prod_id, MAX(rating) as 'top_rating' FROM table_name GROUP BY prod_id;
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 04-05-2006, 01:43 PM THREAD STARTER               #4 (permalink)
ccTLD Specialist
 
Smooth's Avatar
Join Date: Jan 2006
Posts: 802
Smooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of light
 


Cystic Fibrosis Cystic Fibrosis Cystic Fibrosis
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.
Smooth is offline  
Old 04-05-2006, 02:02 PM   #5 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,963
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
Using a subquery to determine the max rating for each record...
PHP Code:
SELECT record_idprod_nameprod_idrating
FROM your_table
WHERE your_table
.rating = (
(

SELECT maxrating 
FROM your_table your_table2
WHERE your_table2
.prod_id your_table.prod_id
)

For this example I have assumed your table is called 'your_table'. Please note that in the subquery 'your_table' is aliased as 'your_table2'. Just replace 'your_table' in this code with your actual table name and you'll be fine...
????: 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
sdsinc is online now  
Old 04-05-2006, 11:02 PM   #6 (permalink)
Senior Member
 
Constantin's Avatar
Join Date: Jan 2006
Posts: 1,127
Constantin has much to be proud ofConstantin has much to be proud ofConstantin has much to be proud ofConstantin has much to be proud ofConstantin has much to be proud ofConstantin has much to be proud ofConstantin has much to be proud ofConstantin has much to be proud of
 



Originally Posted by sdsinc
Also, please note that you need at least mySQL 4.0 (?) to do subqueries...
At least 4.1

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
Constantin is offline  
Old 04-06-2006, 12:09 PM THREAD STARTER               #7 (permalink)
ccTLD Specialist
 
Smooth's Avatar
Join Date: Jan 2006
Posts: 802
Smooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of lightSmooth is a glorious beacon of light
 


Cystic Fibrosis Cystic Fibrosis Cystic Fibrosis
Originally Posted by sdsinc
Using a subquery to determine the max rating for each record...
PHP Code:
SELECT record_idprod_nameprod_idrating
FROM your_table
WHERE your_table
.rating = (
(
????: NamePros.com http://www.namepros.com/showthread.php?t=184138

SELECT maxrating 
FROM your_table your_table2
WHERE your_table2
.prod_id your_table.prod_id
)

For this example I have assumed your table is called 'your_table'. Please note that in the subquery 'your_table' is aliased as 'your_table2'. Just replace 'your_table' in this code with your actual table name and you'll be fine...

Also, please note that you need at least mySQL 4.0 (?) to do subqueries...

Hi dude, thanks. It works fine. 25np$ on the way!
__________________
All offers from me are valid for 24 Hrs unless otherwise noted.
Smooth is offline  
Old 04-06-2006, 12:48 PM   #8 (permalink)
Domains my Dominion
 
sdsinc's Avatar
Join Date: Aug 2005
Location: Web 1.0
Posts: 9,963
sdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatnesssdsinc Has achieved greatness
 


Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV Animal Rescue Wildlife Breast Cancer Animal Rescue Wildlife
Thanks for the NP$, glad it helps
__________________
NameNewsletter.com - free lists of available domain names
ZoneFiles.net (beta) - ccTLD and gTLD droplists
sdsinc is online now  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:11 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger