tags:

views:

303

answers:

3

I have a table user_quotes with the fields quotes_id, quotes_user, quotes_desc, quotes_date, quotes_status, quotes_location. In this quotes_user allows duplication entries. when i execute my query i am trying to avoid duplication entries of quotes_user. so I executed the query like this,

select distinct quotes_user from user_quotes;

This query returning only quotes_user field. How can I retrieve all other records using distinct quotes_user.

I have tried with these following,

select distinct quotes_user, quotes_desc, quotes_date, quotes_status from user_quotes;

Its not avoiding the duplication of quotes_user.

If i use,

select distinct quotes_user, * from user_quotes;

I am getting mysql error,

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* FROM `user_quotes`

Any Ideas how can i fetch all records using select distinct of a single column in a same table. I am storing the email address in the field. the datatype is varchar. Thanks.

Note : Pls dont suggest me other types like group by or other. I need to know how can i retrieve the records by only using distinct. thanks.

A: 

It sounds like quotes_user should be a foreign key like user_id to presumably your users table. You could then query user_quotes by user_id returning all the quotes for that user. Your front end could then format all the quotes for each user nicely, it doesn't really sound like a MySql issue.

r-dub
+2  A: 

You say you want to retrieve the other fields, but you haven't specified how SQL is to know which values to retrieve for the other fields, for each distinct value of quotes_user.

To show you want I mean, consider this example:

+-------------+---------------+
| quotes_user | email_address |
+-------------+---------------+
| user1       | email1        |
| user1       | email2        |
| user2       | email3        |
| user2       | email4        |
| user2       | email5        |
| user3       | email6        |
+-------------+---------------+

Now, if you just wanted quotes_user, the output would obviously be:

+-------------+
| quotes_user |
+-------------+
| user1       |
| user2       |
| user3       |
+-------------+

But if you wanted the other fields as well, you'd need to decide whether, for example, to have email1 or email2 for the user1 row.

Perhaps what you want is to concatenate the values of the other fields together. In that case, I would suggest using the GROUP_CONCAT aggregate function with GROUP BY quotes_user.

I'm not sure why you want to avoid using GROUP BY, though. Perhaps if you could explain that, we could help more.

David
hey, did you write those ascii tables by hand or is there a tool available to generate them?
Dale Halliwell
thanks for the reply. I got the result when i have tried with group by. but in the same way why cant i get the results i have tried with distinct. how can i achieve the same result, done using group by with the distinct.
paulrajj
You can't. Read my answer.
Arthur Reutenauer
@Dale Halliwell: I just did them by hand (with the help of a bit of cut-and-paste, of course). I wouldn't be surprised if there was a tool do them, though.
David
+2  A: 

In addition to what has already been said, it should be stressed that the DISTINCT keyword can't give you distinct results for an individual column when you're selecting multiple columns. You will get distinct rows of, in your case, 4 columns. What you're observing is the expected, standard behaviour of DISTINCT.

Arthur Reutenauer
ok. Can u elaborate with my example how can i achieve all records by using disitinct in any other way ? by using join like ?
paulrajj
I can't really think of anyway to achieve what you want using `DISTINCT`. This can be done very easily with `GROUP BY`. Why are you so attached to using `DISTINCT`?
Arthur Reutenauer
oh.. fine. there is no reason why i am looking for this distinct question. As i got the result by using GROUP BY, i just want to know how can i get the results using distinct. If some one provide me some links or tutorials to learn in deep about distinct, will be helpful to me. thanks.
paulrajj
I got the results by using Distinct, exactly what i am looking for. check out the Quassnoi's Answer in http://stackoverflow.com/questions/612231/sql-select-rows-with-maxcolumn-value-distinct-by-another-column . Thanks for your replies.
paulrajj