[advanced search]
Results from the most recent live auction are here.
21 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Domain Name Industry Newsletter
Go Back   NamePros.Com > Design and Development > Programming
User Name
Password

Old 04-05-2006, 02:02 PM   · #1
Smooth
ccTLD Specialist
 
Smooth's Avatar
 
Trader Rating: (33)
Join Date: Jan 2006
Posts: 830
NP$: 86.85 (Donate)
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 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


Please register or log-in into NamePros to hide ads
__________________
All offers from me are valid for 24 Hrs unless otherwise noted.
Smooth is offline   Reply With Quote
Old 04-05-2006, 02:15 PM   · #2
Jim_
NamePros Regular
 
Jim_'s Avatar
 
Name: Jim, of course
Location: Philadelphia
Trader Rating: (20)
Join Date: Aug 2005
Posts: 558
NP$: 14.30 (Donate)
Jim_ is a glorious beacon of lightJim_ is a glorious beacon of lightJim_ is a glorious beacon of lightJim_ is a glorious beacon of lightJim_ is a glorious beacon of light
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?
__________________
woop woop
Jim_ is offline   Reply With Quote
Old 04-05-2006, 02:26 PM   · #3
Noobie
DNOA Member
 
Noobie's Avatar
 
Name: Shoei
Location: Montreal, Quebec, Canada
Trader Rating: (0)
Join Date: Feb 2006
Posts: 324
NP$: 65.00 (Donate)
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   Reply With Quote
Old 04-05-2006, 02:43 PM   · #4
Smooth
ccTLD Specialist
 
Smooth's Avatar
 
Trader Rating: (33)
Join Date: Jan 2006
Posts: 830
NP$: 86.85 (Donate)
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 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   Reply With Quote
Old 04-05-2006, 03:02 PM   · #5
sdsinc
Law and disorder
 
sdsinc's Avatar
 
Name: Kate
Location: Expat
Trader Rating: (57)
Join Date: Aug 2005
Posts: 5,481
NP$: 1585.11 (Donate)
sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute
Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV
Using a subquery to determine the max rating for each record...
PHP Code:
SELECT record_id, prod_name, prod_id, rating
FROM your_table
WHERE your_table
.rating = (
(

SELECT max( rating )
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...
sdsinc is offline   Reply With Quote
Old 04-06-2006, 12:02 AM   · #6
Constantin
DNOA Member
 
Constantin's Avatar
 
Trader Rating: (63)
Join Date: Jan 2006
Posts: 1,069
NP$: 181.75 (Donate)
Constantin is a splendid one to beholdConstantin is a splendid one to beholdConstantin is a splendid one to beholdConstantin is a splendid one to beholdConstantin is a splendid one to beholdConstantin is a splendid one to beholdConstantin is a splendid one to behold
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:
http://dev.mysql.com/doc/refman/4.1...subqueries.html
Constantin is offline  
  Reply With Quote
Old 04-06-2006, 01:09 PM   · #7
Smooth
ccTLD Specialist
 
Smooth's Avatar
 
Trader Rating: (33)
Join Date: Jan 2006
Posts: 830
NP$: 86.85 (Donate)
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 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_id, prod_name, prod_id, rating
FROM your_table
WHERE your_table
.rating = (
(

SELECT max( rating )
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   Reply With Quote
Old 04-06-2006, 01:48 PM   · #8
sdsinc
Law and disorder
 
sdsinc's Avatar
 
Name: Kate
Location: Expat
Trader Rating: (57)
Join Date: Aug 2005
Posts: 5,481
NP$: 1585.11 (Donate)
sdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond reputesdsinc has a reputation beyond repute
Third World Education Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Find Marrow Donors! Animal Rescue Animal Cruelty AIDS/HIV
Thanks for the NP$, glad it helps
sdsinc is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Hunting Moon RealTechNetwork Arcade Script
Advertise your business at NamePros
All times are GMT -7. The time now is 10:30 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0