Solved

SELECT DISTINCT on one column only

Posted on 2015-01-08
4
100 Views
Last Modified: 2015-01-08
How do I select distinct only on one column.

Below is my current code...

$query_rs_media_new_group = "SELECT DISTINCT title, group_id, description, brand, weight, category, id FROM media_new";

Open in new window


My issue is that I need to select all the columns you see in my code. However I only want the "title" column to be DISTINCT.

What do I need to do?
0
Comment
Question by:petewinter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40538617
Would you provide some sample data and desired results?

Thanks!
0
 

Author Comment

by:petewinter
ID: 40538709
See sample data attached.

media-new.csv

You will notice in the data some of the "title" rows are the same. I only want to show the first unique "title".  So I should only see 8 of the 30 rows of data in the sample data.
0
 
LVL 32

Accepted Solution

by:
Daniel Wilson earned 500 total points
ID: 40538772
You want each title but only one of each of the other fields.  OK, so we'll pick ... one of them.  We can pick the smallest or the largest.  If you want to switch to the largest, change the "MIN" to "MAX".
$query_rs_media_new_group = "SELECT title, MIN(group_id) as group_id, min(description) as description, min(brand) as brand, min(weight) as weight, min(category) as category, min(id) as id 
FROM media_new group by title";

Open in new window

0
 

Author Closing Comment

by:petewinter
ID: 40538840
Many thanks
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question