Jump to content

Select only one occurence


Ninjakreborn

Recommended Posts

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

$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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.