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.

  • First, looks like to use categories is a better solution than to use fields for cities. Second, one entry per city? – Robson Sobral Jun 5 '13 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? – Justin Long Jun 5 '13 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 Jun 6 '13 at 11:42
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.

  • just updated my query. Forgot DISTINCT within SQL. – Bhashkar Yadav Jun 6 '13 at 10:56
  • This should do the trick! Thank you! – mediapimp Jun 6 '13 at 11:43
  • Yep, DISTINCT is probably faster than GROUP BY – foamcow Jun 6 '13 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

  • GROUP BY field_id_xxx will result in identical output to performing a DISTINCT field_id_xxx in the SELECT, in this instance... – Joshua Burns Feb 26 '15 at 21:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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