ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Will a custom SQL query do this? DISTINCT entries by date custom field

January 27, 2011 12:22pm

Subscribe [2]
  • #1 / Jan 27, 2011 12:22pm

    julie p

    282 posts

    Hi all. I have a question about whether SQL can accomplish this or I have to do something else. I have a magazine that has 12 editions per year. Each Edition has an {edition-date}, a custom date field (Jan 01, 2010, Feb 01, 2010, etc…).

    In the archives section, I want to have a dropdown menu with only the available years of existing Editions. Not the entry date (so {exp:yearly_archives} will not work).

    I’ve tried this:

    {exp:query sql='SELECT DISTINCT field_id_12 AS year FROM exp_weblog_data WHERE field_id_12 <> ""'}
        <option value="{year}">{year format="%Y"}</option
    {/exp:query}

    Which works perfectly, but just isn’t what I want since it is spitting out 2010 12 times, etc… For instance, Jan->December will all have unique entry-dates, but they are all 2010, for instance. Is there a way to interpret each date and limit accordingly? If not - other ideas other than hard-coding?

    Thanks!

  • #2 / Jan 27, 2011 2:43pm

    julie p

    282 posts

    from my SQL guru..

    {exp:query sql='SELECT DISTINCT year(from_unixtime(field_id_12)) AS year FROM exp_weblog_data'}
        <option value="{year}">{year}</option
    {/exp:query}
  • #3 / Jan 27, 2011 3:10pm

    Ingmar

    29245 posts

    So, you’re good, i.e. just sharing your solution with us?

  • #4 / Jan 27, 2011 3:20pm

    julie p

    282 posts

    yep 😊

  • #5 / Jan 27, 2011 3:37pm

    Ingmar

    29245 posts

    Cool 😊 Closing.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases