开发者

mysql select query pattern, get duplicates just once , regex, pattern

开发者 https://www.devze.com 2023-01-05 15:26 出处:网络
I have a database with 3 columns (item_number, seller_id, type) . I\'m getting the columns content using while loop.

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开发者_StackOverflow社区 last) combination of seller_id => item_number.


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'
0

精彩评论

暂无评论...
验证码 换一张
取 消