Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

Directory Submission Service   I Sell Pagerank   V7N Directory

Reply
 
LinkBack Thread Tools Display Modes
Old 12-29-2007, 02:43 PM   #1 (permalink)
Contributing Member
 
lordspace's Avatar
 
Join Date: 05-30-06
Location: Canada
Posts: 458
iTrader: 0 / 0%
lordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nice
Send a message via ICQ to lordspace Send a message via Skype™ to lordspace
Retrieve products by groups i.e. by price range

I am trying to retrieve products by groups, i.e. by price range: 0-10; 10.01-19.99

Is there another/better way to do that ?

Code:
SELECT CONCAT('$0-$9.99', ' (', count(*), ')') as price_range FROM `PREFIX_products` WHERE sale_price between 0 and 9.99 UNION ALL SELECT CONCAT('$10-$19.99', ' (', count(*), ')') as price_range FROM `PREFIX_products` WHERE sale_price between 10 and 19.99
lordspace is offline  
Add Post to del.icio.us
Reply With Quote
Sponsored Links
SEO Hosting by HostGator  Advertise Here  Buy Blog Links
Old 12-30-2007, 05:51 AM   #2 (permalink)
Contributing Member
 
Join Date: 09-01-07
Location: Gurgaon, India
Posts: 85
iTrader: 0 / 0%
kaykays is on the right pathkaykays is on the right path
You can try CASE, some thing like this:
PHP Code:
SELECT 
CASE sale_price
    WHEN sale_price between 0 
and 9.99 THEN '$0-$9.99'
    
WHEN sale_price 10 and 19.99 THEN '$10-$19.99'
END AS pricegroup
COUNT(*)
FROM `PREFIX_products`
GROUP BY pricegroup 
kaykays is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-30-2007, 11:51 AM   #3 (permalink)
Contributing Member
 
lordspace's Avatar
 
Join Date: 05-30-06
Location: Canada
Posts: 458
iTrader: 0 / 0%
lordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nicelordspace is just really nice
Send a message via ICQ to lordspace Send a message via Skype™ to lordspace
Thanks for the suggestion.

I've modified it a little bit and here are two of the solutions.

Note: When storing FLOAT values in DB be aware that values may not be represented as we think e.g. 3.40 can be internally represented as 3.399999999 and the different can be seen at the very last digit.

Here is a nice explanation and we all should be aware of that:
http://dev.mysql.com/doc/refman/5.0/...ith-float.html

Select products count for given price range

Code:
SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 0 and 9.9999 UNION ALL SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 10 and 19.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 20.00 and 29.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 30 and 39.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 40 and 49.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 50 and 99.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 100 and 149.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 150 and 299.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price between 300 and 499.9999 UNION SELECT count(*) as price_group FROM `PREFIX_products2` WHERE sale_price > 500;
Code:
SELECT CASE WHEN sale_price BETWEEN 00 and 09.9999 THEN 1 WHEN sale_price BETWEEN 10 and 19.9999 THEN 2 WHEN sale_price BETWEEN 20 and 29.9999 THEN 3 WHEN sale_price BETWEEN 30 and 39.9999 THEN 4 WHEN sale_price BETWEEN 40 and 49.9999 THEN 5 WHEN sale_price BETWEEN 50 and 99.9999 THEN 6 WHEN sale_price BETWEEN 100 and 149.9999 THEN 7 WHEN sale_price BETWEEN 150 and 299.9999 THEN 8 WHEN sale_price BETWEEN 300 and 499.9999 THEN 9 WHEN sale_price > 500 THEN 10 ELSE 0 END AS price_group, count(*) as group_count FROM `PREFIX_products2` GROUP BY price_group ORDER BY price_group
lordspace is offline  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



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

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search / Retrieve document box usability ideas Skags Web Design Lobby 3 11-29-2007 12:30 PM
A good price range. help me out here :) wulf2001 Web Design Lobby 5 07-19-2006 11:17 AM
how to retrieve results from Database on specific field swcobra Coding Forum 3 06-29-2006 03:15 PM
can't retrieve my password on hostdetective phosting Web Hosting Forum 2 06-21-2004 08:13 PM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 08:23 PM.
© Copyright 2008 V7 Inc