Ninjakreborn Posted December 5, 2011 Share Posted December 5, 2011 This is driving me crazy. I have a table that has a lot of data in it. I have a field called Order ID that has multiple occurrences of each order ID. I need to be able to get 1 record with Each order ID. I basically tried using distinct..and it's still returning multiple occurrences of each order ID. $result = mysql_query("SELECT DISTINCT order_id FROM jp_transaction_history WHERE last_name LIKE '%" . $last_name . "%' AND transaction_type = 'ss'"); ALL I want to do is get 1 record for every Order ID that is found..not duplicate order ID's. Any advice, is appreciated. Link to comment https://forums.phpfreaks.com/topic/252512-select-only-one-occurence/ Share on other sites More sharing options...
Ninjakreborn Posted December 5, 2011 Author Share Posted December 5, 2011 $result = mysql_query("SELECT DISTINCT order_id, first_name, last_name, city, phone, state, product_sku FROM jp_transaction_history WHERE last_name LIKE '%" . $last_name . "%'"); This is what I currently have...it's not working. Any advice is appreciated. Link to comment https://forums.phpfreaks.com/topic/252512-select-only-one-occurence/#findComment-1294637 Share on other sites More sharing options...
PFMaBiSmAd Posted December 5, 2011 Share Posted December 5, 2011 DISTINCT removes duplicate rows from the result set. Since the product_sku is probably different in each row, the the rows are all different and there's nothing for distinct to remove. If all you are trying to get is a list of distinct order_id's, why are you selecting other columns? Use GROUP BY order_id to consolidate all rows having the same order_id into one row in the result set. Link to comment https://forums.phpfreaks.com/topic/252512-select-only-one-occurence/#findComment-1294638 Share on other sites More sharing options...
Ninjakreborn Posted December 5, 2011 Author Share Posted December 5, 2011 I understand now the entire difference between Group BY and distinct. Thanks..I have used group by before, but I didn't realize it worked in the way you described. Thanks for sharing that with me, it's working perfectly now. Link to comment https://forums.phpfreaks.com/topic/252512-select-only-one-occurence/#findComment-1294644 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.