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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.