User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 391,794 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,507 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 571 | Replies: 9
Reply
Join Date: Jan 2008
Posts: 9
Reputation: nomadhacker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nomadhacker nomadhacker is offline Offline
Newbie Poster

Help getting list of top rated products

  #1  
Jan 17th, 2008
I am working on a product reviews site using php and mysql, and have a table set up for product data, as well as review data. I am trying to get a list of top rated products within the last 24 hours to show up on the front page. This is proving to be trickier than I thought. Here is my table structure:
producttable
	(
	productid		int(10) 	UNSIGNED	NOT NULL	PRIMARY KEY
	producttitle	char(160)	NOT NULL
	productreleaseyear	year(4)	NOT NULL	INDEX
	productrating	tinyint(3)	unsigned	NOT NULL	INDEX 	default=1
	categoryid		tinyint(3)	UNSIGNED	NOT NULL	INDEX
	productnumberreviews	int(10)	UNSIGNED	NULL		INDEX
	)

reviewtable
	(
	reviewid	int(10)		UNSIGNED	NOT NULL	PRIMARY KEY
	userid		int(10)		UNSIGNED	NOT NULL	INDEX
	reviewtext	text		NOT NULL
	reviewtitle	char(160)	NOT NULL	
	productid		int(10)		UNSIGNED	NOT NULL	INDEX
	productrating	tinyint(3)	UNSIGNED	NOT NULL	default=1
	reviewdate	datetime	NOT NULL	INDEX
	) 

Does anyone have any tips or ideas on this?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: getting list of top rated products

  #2  
Jan 17th, 2008
  1. SELECT * FROM reviewtable WHERE reviewdate >= DATE_SUB(NOW(), INTERVAL 24 HOUR) ORDER BY productrating desc

I think this should work. I am selecting only those records where reviewdate is greater than (present-time - 24 hours).
P.S. I haven't tested it. But I think it will work.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 9
Reputation: nomadhacker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nomadhacker nomadhacker is offline Offline
Newbie Poster

Re: getting list of top rated products

  #3  
Jan 18th, 2008
This actually helps make things a lot easier in terms of the time encoding, and will cut out some php programming for that, thank you. I had been planning on calculating the date in php, this should speed things up a lot.

I guess I'm looking at loading all those into an array and just calculating the top 10 movies in php. I had been hoping someone would have an idea on a way to trim off some processing time, by performing some of that calculation in the query, but now that I look at the problem again, there doesn't appear to be any way to do that.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: getting list of top rated products

  #4  
Jan 18th, 2008
This would list all the records in the reviewtable in descending order(only those within 24 hrs). But if you want only 10 rows, then you can use limit.
  1. SELECT * FROM reviewtable WHERE reviewdate >= DATE_SUB(NOW(), INTERVAL 24 HOUR) ORDER BY productrating desc LIMIT 0,10
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 9
Reputation: nomadhacker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nomadhacker nomadhacker is offline Offline
Newbie Poster

Re: getting list of top rated products

  #5  
Jan 18th, 2008
Yes, but the limit's not the problem. There will probably be multiple reviews for the same product in a given day. And I want to rank based upon the average of all the reviews in that 24 hour timeframe. So I'm going to have to first grab all the ratings and corresponding productids from the reviewtable within 24 hours, then I have to calculate the averages, then display the top 10 only. It looks like it's going to be a lot more php code than I had hoped for from a loading time standpoint, because I can get the productid and ratings from the reviewtable, and match them up with the producttitle for each row, but the averaging and final ranking looks like it'll all have to take place in php. That's just an awful lot of data that I was hoping to not have to grab just to throw away.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: getting list of top rated products

  #6  
Jan 18th, 2008
Example,
  1. SELECT AVG(productrating) as average FROM reviewtable GROUP BY productid ORDER BY average desc

Calculates the average of all the products and puts the product with more average on top. I am sure you can make use of that.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 9
Reputation: nomadhacker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nomadhacker nomadhacker is offline Offline
Newbie Poster

Re: getting list of top rated products

  #7  
Jan 18th, 2008
Now that's reaaally helpful. If I select other data along with that, will it avoid duplicate data? So say I modify this a little and put:

SELECT AVG(r.productrating) AS average, r.productid AS productid, p.producttitle AS title 
FROM reviewtable AS r 
LEFT JOIN producttable AS p ON r.productid = p.productid 
GROUP BY productid ORDER BY average desc LIMIT 10
Last edited by nomadhacker : Jan 18th, 2008 at 2:32 pm.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: getting list of top rated products

  #8  
Jan 18th, 2008
umm.. I don't think this would be any problem.. Check that out.. Joins is not my forte! But by looking at the query, I think it will work! (not sure though!)
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jan 2008
Posts: 9
Reputation: nomadhacker is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nomadhacker nomadhacker is offline Offline
Newbie Poster

Re: getting list of top rated products

  #9  
Jan 18th, 2008
much thanks.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: getting list of top rated products

  #10  
Jan 18th, 2008
You are welcome!
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 5:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC