troubleshooting Question

DISTINCT MySQL Query But Exclude A Column From DISTINCT Criteria

Avatar of Ryan Bayne
Ryan BayneFlag for United Kingdom of Great Britain and Northern Ireland asked on
PHPMySQL Server
4 Comments1 Solution922 ViewsLast Modified:
Hey, worded the title as best I could.

I have a DISTINCT query, works great but I now need the records to include the record ID. In my table the column is named eciid. Adding eciid as the query is right now simply makes that column a part of the DISTINCT criteria and returns even more records.

I only need one of the ID's from a group of records sharing the same DISTINCT values from one or two columns (that part does not matter). I just thought I would ask for advice on the best approach to do this.

My query is below and I changed the second line to this...

DISTINCT eciid,'.$columns.'

Then after testing I realise why it cannot be done this way.

Considered querying the table for each returned record from this query to get a single ID for a single record that shares the distinct values. Either way I look at this, there is a lot of queries to be performed in a loop of say 10 DISTINCT records.

Thanks in advance for a recommended approach

$records = $wpdb->get_results( 'SELECT
	DISTINCT '.$columns.' 
	FROM '. $csv['sql']['tablename'] .' 
	WHERE '.$where.'
	LIMIT '.$number.'',ARRAY_A );
	return $records;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros