views:

32

answers:

1

I have a database with 3 columns (item_number, seller_id, type) . I'm getting the columns content using while loop.

$sql = msql_query("SELECT * FROM item_detals WHERE type='classifiedTitlePrice'");
while($row = mysql_fetch_array($sql)) {
    $item_number = $row['item_number'];
    $mcat = $row['mcat'];
}

However the problem is that a seller_id may have more items(e.g example_seller => 1234 , example_seller =>55555) so I would like to know if it's possible to get only an a combination of item_number & seller_id which has the same seller_id .So basically if there will be example_seller => 1234 , example_seller =>55555 the query should get only the 1st (or last) combination of seller_id => item_number.

A: 

Try GROUP BY or DISTINCT

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html

http://dev.mysql.com/doc/refman/5.1/en/distinct-optimization.html

SELECT item_number, seller_id 
FROM item_detals 
WHERE type='classifiedTitlePrice' 
GROUP BY `seller_id`

or

SELECT item_number, DISTINCT seller_id 
FROM item_detals 
WHERE type='classifiedTitlePrice'
Lizard