0

Currently I have this query:

SELECT column1,column2 FROM table

column1 needs to be distinct, column2 does not.

SELECT DISTINCT column1, NON-DISTINCT column2 FROM table

Now I know that doesn't make sense but I need column1 to be distinct and column2 to be anything. How would I do that.

2
  • 1
    Do you want a single bla1 at random whenever you have two or more PIDs? Commented Sep 11, 2009 at 11:37
  • well i don't want to do "select * from table" because i only want to get back "pid" and "bla1" - where 'pid' is distinct and 'bla1' isn't. the outcome being rows 1-3 Commented Sep 11, 2009 at 11:54

2 Answers 2

2
select pid, group_concat(distinct bla1) as bla1s
from table
group by pid;

The above will get you 1 row for each pid and you'll be able to see if there are extra bla1s without introducing a new column or having to settle for a random choice of multiple bla1s.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this (fastest):

SELECT *
FROM `table`
GROUP BY pid
HAVING min( id )

second (slower) option:

select *
from `table` t1
where
    t1.id = (select min(id) from `table` t2 where t1.pid = t2.pid)

7 Comments

:( isn't there a way to do it without the "group/min"?
Why don't you want the grouping? SELECT DISTINCT is for getting unique values only, other values you try and select will be ambiguous. Should it select 2 or 7 as the bla1?
There is no way how to do that without group, or there is - subselect, but this will be slow. Or you can delete these records if you don't want them. Why you don't want to use group by?
goat -> i want to do this (select pid,bla1 from table) but i only want pid to be distinct // malek -> because i think there is an easier way to do it.
without group by, but not easier :-) I think you will not find easier way: select * from test t1 where t1.id = (select min(id) from test t2 where t1.pid = t2.pid)
|

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.