Page 1 of 1

select from loop

Posted: Mon Mar 27, 2006 4:10 pm
by aceconcepts
Hi,

I have a mysql database and one table is a 'product' table.

This table consists of product names, prices and types.

What i would like to be able to do is display a list of each product type that is different.

For example:

product table consists of

Name Price Type
------------------------------------------------------
giraffe £1.00 mask
lion £1.00 mask
whistle £2.00 toy
ball £1.50 toy
t-shirt £5.50 clothing
------------------------------------------------------

I want to be able to display a list of each different product type.

e.g. from the table above the list would look like:

List
--------------
mask
toy
clothing
--------------

How can i do this?

Posted: Mon Mar 27, 2006 4:22 pm
by s.dot
select * from table where type = 'toy'

Posted: Mon Mar 27, 2006 4:27 pm
by aceconcepts
That won't work because i want to extract all different product 'type' without specifying one.

Posted: Mon Mar 27, 2006 4:40 pm
by s.dot
ooooh ok

Code: Select all

$types = array('toy','clothing','mask');
foreach($types AS $type){
   SELECT * FROM `table` WHERE `type` = $type;
}

// or
// kidna guessing on this one, sorry 
SELECT * FROM `table` GROUP BY `type`

Posted: Mon Mar 27, 2006 4:45 pm
by feyd

Code: Select all

SELECT DISTINCT `type` FROM `table`