Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using DISTINCT in one column but not in others

Status
Not open for further replies.

sbayter

IS-IT--Management
Nov 14, 2002
95
CO
Hi,
I have this page that is displaying 2 columns, one is the date in which the document was downloaded and the other the email of the user.
I need to show only unique emails but if I use DISTINCT it also looks for unique dates.
This is what I have:
SELECT DISTINCT EMAIL, DATE FROM logs

If I take the DATE column out, it works perfect but I need it.
Is there anyway to select unique emails and that it only show the first date in the query?
Thanks in advance,

sbayter
 
Define what is the "first date".

If it's the earliest date, something like:

SELECT DISTINCT email, min(the_date) FROM logs group by email order by email


(My column names do not match yours. "Date" is a MySQL reserved word and should not be used as a column name)

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks!!!
I tried that and it worked but I tried to add a "WHERE"
and gave me this error:

[red]You have an error in your SQL syntax near 'WHERE Doc_Name = 'DOC1' LIMIT 0, 100' at line 1 [/red]

I've never use GROUP BY. I think I once read using GROUP BY and WHERE created some conflict and that one had to use HAVING.
This is what I have:
"SELECT DISTINCT EMAIL, Doc_Name, min(myDATE) FROM logs GROUP BY EMAIL ORDER BY $sortby WHERE Doc_Name = '$doc' "

Thanks again!

sbayter
 
Code:
"SELECT DISTINCT EMAIL, Doc_Name, min(myDATE) FROM logs WHERE Doc_Name = '$doc' GROUP BY EMAIL ORDER BY $sortby "

try this ;)
 
Great thanks! It worked!!!!
But why isn't it showing the date?
It just gives an empty field.

sbayter
 
perhaps define a variable name for it

Code:
"SELECT DISTINCT EMAIL, Doc_Name, min(myDATE) as mydatetemp FROM logs WHERE Doc_Name = '$doc' GROUP BY EMAIL ORDER BY $sortby "

I'm not sure but I believe through the min(mydate) mydate itself is no longer recognized

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top