简体   繁体   中英

count number of elements in list mysql

I have a table tab contains events where participants are listed as a comma separated list of ids: 1,5,6,24,124

I want to get the AVG participant number for events only if the resolution is equal to Completed

This is what I tried:

SELECT avg(case when (resolution = 'Completed' then LENGTH(REPLACE(participants, ',', '')) end) participants FROM tab;

but it doesn't work

Fix your data model: Storing lists in a string is wrong for a lot of reasons:

  • Numbers should be stored as numbers not strings.
  • Ids should have properly declared foreign key relationships, which doesn't work when they are in a string.
  • Columns are intended to store single values, not multiple values.
  • SQL has lousy string processing capabilities.
  • SQL has a great data structure for storing lists. It is called a table .

The right thing to do is to fix the data model. Sometimes, you are stuck with someone else's really, really bad data model. In that case, you can count the number of commas and add one:

SELECT AVG( 1 + LENGTH(participants) - LENGTH(REPLACE(participants, ',', ''))) as avg_participants
FROM tab
WHERE resolution = 'Completed';

However, your real effort should be on fixing the data model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM