Solved

SELECT DISTINCT on one column only

Posted on 2015-01-08
4
88 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now