1

I have a Database table in MYSQL, it looks like this:

Project_ID  user_ID  name
11          1        fred
11          2        rick
11          1        fred

I want to get the names in the table, but when I display it I get the same name twice because the user_ID 1 appears twice in the table. I tried to display it with GROUP BY and SUM, but I don't want to do it this way. How do I get it not to return duplicate rows?

2
  • 1
    Show us the query you're trying... Commented Jan 21, 2013 at 14:51
  • How do you want to display them? Separated? Maximum value only? Added to each other? In other words: you need to provide some more details! Commented Jan 21, 2013 at 14:51

3 Answers 3

4

Use DISTINCT

  SELECT DISTINCT user_ID
       , verschil_ma 
    FROM project_uren 
   WHERE project_ID = 11
GROUP BY user_ID
Sign up to request clarification or add additional context in comments.

8 Comments

What happens with the other information from the table? Can you only get 1 value (user_ID) from the table when using DISTINCT?
You can get any values you want. DISTINCT only affects one column and doesn't limit you to retrieving only that column. I'll expand my answer for you to reflect this.
Thank you for your answer. When I'm using it like this: DISTINCT user_id it displays 2 users. When I'm adding name or another value it displays 3 users. (Inclusive duplicates)
Show us your query so we can see exactly what you're doing. That will be a big help to us.
$sql = "SELECT DISTINCT user_ID, verschil_ma FROM project_uren WHERE project_ID=$id";
|
1

Point 1 is that should the user be assigned to the project twice?

Point 2 - Use the DISTINCT keyword to return only unique records - http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html

SELECT DISTINCT user_ID
FROM TABLE
WHERE Project_id = 11

That will return you 1 and 2 (You won't get 1 twice)

Thanks

Comments

-1
$results = // query
$results = array_unique($results);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.