1

Is there a way to only show distinct entries using the channel entries tag?

For example, I want to create a list of channel entries that have a city field. The list would show the city name, but there are several entries that would have the same city name. If I use the channel entries tag to display the cities, their would be duplicates.

How do I avoid the duplicates?

Thanks for any help.

3
  • First, looks like to use categories is a better solution than to use fields for cities. Second, one entry per city?
    – Sobral
    Commented Jun 5, 2013 at 23:31
  • Do you have a code sample of what you are trying to do? Or are you able to provide more details to help describe what you are trying to do? Commented Jun 5, 2013 at 23:31
  • Categories would do the trick but was hoping to avoid using categories. I think the query module may be the ticket. Thanks.
    – mediapimp
    Commented Jun 6, 2013 at 11:42

2 Answers 2

6

If you would like to avoid the duplicates while showing only cities, you can use Query module to populate the cities. Like:

{exp:query sql="SELECT DISTINCT field_id_xxx AS city_name FROM exp_channel_data WHERE channel_id=x"}

{city_name}

{/exp:query}

You need to replace field_id_xxx with correct custom field id.

2
  • just updated my query. Forgot DISTINCT within SQL. Commented Jun 6, 2013 at 10:56
  • Yep, DISTINCT is probably faster than GROUP BY
    – foamcow
    Commented Jun 6, 2013 at 16:50
0

As Bhashkar says, you can use the query module to do this. However to avoid showing duplicates you will need to add a GROUP clause;

{exp:query sql="SELECT field_id_xxx AS city_name FROM exp_channel_data WHERE channel_id=x GROUP BY field_id_xxx"}

{city_name}

{/exp:query}

This will ensure that each city name is shown only once

1
  • GROUP BY field_id_xxx will result in identical output to performing a DISTINCT field_id_xxx in the SELECT, in this instance... Commented Feb 26, 2015 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.