Solved

MySQL Distinct question

Posted on 2012-04-04
10
432 Views
Last Modified: 2012-04-05
I have a question about the "distinct" syntax.

In this example:
Select distinct pID, name, address, phone, email from table;

Open in new window


I only want the distinct pID.

In this query, would my results show distinct values for pID, name, address, phone, and email or JUST pID?
0
Comment
Question by:lvollmer
  • 5
  • 3
  • 2
10 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 37807874
That query will show all of the distinct rows between all values listed.

If you want to just see the unique PID, you need to just have the columns that you want to be distinct in the select.  If you need to join it back to the source table, you can make it a derived table and join it.
0
 

Author Comment

by:lvollmer
ID: 37807890
Here is my real life query:
SELECT
    pd.person_id, pd.first,pd.email,sod.option_text
FROM
    pd
INNER JOIN
    srf ON pd.person_key = srf.person_key
INNER JOIN
    ON srf.survey_option_key =  sod.survey_option_key
WHERE
    srf.survey_option_key IN (297,298,299)
AND
    srf.survey_question_key = '43'
AND
    pd.is_subscriber ='1'
ORDER BY
    pd.person_id ASC

Open in new window


This works fine, except that it returns rows multiple times for pd.person_id. I am not sure how to apply what you suggested to make sure ONLY pd.person_id is distinct. Any idea?
0
 
LVL 18

Expert Comment

by:lludden
ID: 37808111
This will do an inner join to just the distinct ID list so they only display once.
SELECT
    pd.person_id, pd.first,pd.email,sod.option_text
FROM
    pd
INNER JOIN
    srf ON pd.person_key = srf.person_key
INNER JOIN
    ON srf.survey_option_key =  sod.survey_option_key
INNER JOIN (SELECT DISTINCT Person_id FROM PD) PDList ON pd.Person_ID = PDList.Person_id
WHERE
    srf.survey_option_key IN (297,298,299)
AND
    srf.survey_question_key = '43'
AND
    pd.is_subscriber ='1'
ORDER BY
    pd.person_id ASC

Open in new window

0
 

Author Comment

by:lvollmer
ID: 37808164
thanks - it is running, so i will report back when finished.
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 37810269
Hi lvollmer,

Isn't this enough:

Select  pID, name, address, phone, email from table group by pID, name, address, phone, email
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:lvollmer
ID: 37811612
Murphey - woudln't that still show multiple rows for one pID?
0
 
LVL 16

Accepted Solution

by:
theo kouwenhoven earned 500 total points
ID: 37811716
Assuming that pID = PersonalID
and looking to your select, the combination pID and name  etc will be unique,

If not, it makes no sence to select an pID with a name that (maybe) not belongs to that pID or
is one of many that belongs to that pID.

So if pID is connected to one person, the answer is NO

if a pID is connected to more then one person, the answer is Yes, but is that out put still reliable?
0
 

Author Comment

by:lvollmer
ID: 37811822
I will run it now and test.

In the real query I am running (see second query) the result set will return multiple rows for pd.person_id if they have values for this:

WHERE
    srf.survey_option_key IN (297,298,299)
0
 

Author Comment

by:lvollmer
ID: 37812785
this worked, thank you. I guess I did not understand how GROUP BY worked, and it is perfect for this instance.
0
 
LVL 16

Expert Comment

by:theo kouwenhoven
ID: 37814123
GROUP BY wil select each record that has a unique group by,
if your tabel contains 2 rows with the same pID for 1 person with 2 adresses, the line is shown twice
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

12 Experts available now in Live!

Get 1:1 Help Now