2

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.

6
  • Is your table really called 'item_detals' or is this a typo? Commented Jul 5, 2010 at 23:10
  • is a typo . It's "item_details" Commented Jul 5, 2010 at 23:13
  • @Michael: Is the combination (seller_id, item_number) unique for rows where type='classifiedTitlePrice'? Commented Jul 5, 2010 at 23:19
  • @Mark Byers . Not always . Some item numbers are "featured" and they may be stored by 2 or more times (but always with the same seller_id). Commented Jul 5, 2010 at 23:23
  • This is a really poorly-worded question. Could somebody clean it up a bit--e.g. replace the second paragraph with: "The problem is that a seller may have more than one item, so I'd like to know if it's possible to only retrieve one record per seller." Also, what does any of this have to do with regular expressions? Commented Jul 6, 2010 at 1:03

1 Answer 1

2

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.