PowWeb Forums - The Perfect Community for the Perfect Host  

Register now to interact with over 11,000 members! Registered users have Posting Privileges, free access to Private Messaging, Email Notifications and more.

Go Back   PowWeb Community Forums > The PowWeb Platform > MySQL
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Closed Thread
 
Thread Tools Search this Thread
Old 1-6-05, 02:16 PM   #1
notset4life
 
Join Date: Feb 2002
Posts: 352
Reputation: 27
PHP Mysql Query Question

Can someone help me with a PHP mysql query?
I have a database which basically has fields such as

transaction_id and product name

I would like to show on a certain page "Best Sellers", so I need a sql query (in PHP) to produce this. It would have to separate the product names, then add up the total of each so best sellers would be shown.

Example Database:
-------------------
trans_id Product Name
1 Widgets
2 Things
3 Widgets
4 Otherthings
-----------------------
and now on a page show:

BEST SELLERS
Widgets 2
Things 1
Other Things 1

Any help would be appreciated.

Thanks
notset4life is offline  
Old 1-6-05, 04:30 PM   #2
JGross
 
JGross's Avatar
 
Join Date: Sep 2002
Location: Evansville Indiana
Posts: 220
Reputation: 25
are you adding up the number associated with the best sellers? If so where are you getting this number? Is it another field in the table or are you adding it in manually?

i need more information to be able to help you.
__________________
JGross is offline  
Old 1-6-05, 05:06 PM   #3
notset4life
 
Join Date: Feb 2002
Posts: 352
Reputation: 27
Thanks for responding. I don't know the answer, that's what I'm trying to find out the best way to do this.

I am wondering is there is a command that can count how many entries in the product field there are, so if "widgets" came up 12 times, then that would be used to compare the other products to see which are the best sellers.
notset4life is offline  
Old 1-6-05, 05:20 PM   #4
mitchind
Older not wiser
 
mitchind's Avatar
 
Join Date: Nov 2003
Location: Calgary, AB
Posts: 2,473
Reputation: 205
You can do it in one query:
PHP Code:
SELECT count(`transaction_id`)  AS popularity, `product name`
FROM  `products
GROUP  BY transaction_id
ORDER  BY popularity DESC 

and if you want the Top <n> bestsellers just add the LIMIT clause to the end
PHP Code:
SELECT count(`transaction_id`)  AS popularity, `product name`
FROM  `products
GROUP  BY transaction_id
ORDER  BY popularity DESC 
LIMIT 5 

Last edited by mitchind : 1-6-05 at 05:27 PM. Reason: Add code for Top # of entries
mitchind is offline  
Old 1-9-05, 10:11 PM   #5
notset4life
 
Join Date: Feb 2002
Posts: 352
Reputation: 27
Quote:
Originally Posted by mitchind
and if you want the Top <n> bestsellers just add the LIMIT clause to the end
PHP Code:
SELECT count(`transaction_id`)  AS popularity, `product name`
FROM  `products
GROUP  BY transaction_id
ORDER  BY popularity DESC 
LIMIT 5 

Thanks a lot, the query works perfectly in phpmyadmin. Can you tell me how you would place the output on a web page? The code came out to:

$sql = 'SELECT count(`title`) AS popularity, `title`'
. ' FROM `stats` '
. ' GROUP BY title'
. ' ORDER BY popularity DESC '
. ' LIMIT 20';

but I don't know how to show the results. Sorry for my lack of knowledge, but I do appreciate any help.
notset4life is offline  
Old 1-9-05, 11:03 PM   #6
notset4life
 
Join Date: Feb 2002
Posts: 352
Reputation: 27
Never mind, I figured it out, thanks again.
notset4life is offline  
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:43 PM.


Contents ©PowWeb, Inc. ~ vBulletin, Copyright © 2000-2007 Jelsoft Enterprises Limited.