Jump to content

grab unique rows


aebstract

Recommended Posts

$query = mysql_query("SELECT * FROM calendar WHERE MONTH(caldate) = '$calmonth2' && YEAR(caldate) = '$calyear' ORDER BY DAY(caldate) LIMIT 1") or DIE(mysql_error());

 

I have 4 rows, they have a field called event_tag. I want to grab 1 result from each unique event_tag. My query is obviously wrong, but I'm hoping I'm close and not much needs to be changed. Thanks

Link to comment
Share on other sites

I have 4 rows, they have a field called event_tag. I want to grab 1 result from each unique event_tag. My query is obviously wrong, but I'm hoping I'm close and not much needs to be changed. Thanks

 

How do you decide which of the 4 rows for an event tag that you want to bring back. If you just want a list of the event tags then the code Hybride gives should do fine. However if you want the (say) latest row of the 4 for an event tag then something different is required (and could do with the table layout to help there).

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Cheap and nasty way to do it

 

$query = mysql_query("SELECT * FROM calendar WHERE MONTH(caldate) = '$calmonth2' && YEAR(caldate) = '$calyear' ORDER BY DAY(caldate) GROUP BY event_tag LIMIT 1") or DIE(mysql_error());

 

Won't work in most flavours of SQL but MySQL seems to manage it.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

:'( . Made a mistake. The ORDER BY clause has to go after the GROUP BY clause

 

$query = mysql_query("SELECT * FROM calendar WHERE MONTH(caldate) = '$calmonth2' && YEAR(caldate) = '$calyear' GROUP BY event_tag ORDER BY DAY(caldate) LIMIT 1") or DIE(mysql_error()); 

 

Although I suspect you don't want the LIMIT 1 there (which was in your original SQL.

 

$query = mysql_query("SELECT * FROM calendar WHERE MONTH(caldate) = '$calmonth2' && YEAR(caldate) = '$calyear' GROUP BY event_tag ORDER BY DAY(caldate)") or DIE(mysql_error()); 

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.