How do I add a count column to my table?

How do I add a count column to my table?

rob1strob1st Posts: 84Questions: 22Answers: 0

I would like to add a column that shows how many of the main item in the row are in the database.

Is there a way to do this in Editor?

Equivalent of SQL: SELECT COUNT(*) FROM table WHERE field = condition

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin

    Do you mean a query per row? If so, you'd be best served using Mjoins and then displaying the length of the array in the DataTable.

    Allan

  • rob1strob1st Posts: 84Questions: 22Answers: 0

    Not sure this help me, apologies if I don't understand that functionality fully.

    Here is a pic of my old table, as you can see I have outlined my count column. Can Mjoins do that?

  • rob1strob1st Posts: 84Questions: 22Answers: 0

    Previously, with a coded table, I could either run a separate count query within the while loop to generate the counts, or I could use "COUNT(CASE WHEN this = that THEN 1 END)" inside the existing query.

    I am looking to do the same inside my editor datatable.

    Hope that explains better.

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    edited June 2021 Answer ✓

    Hi,

    In the Mjoin example I linked to, the Permissions column could show a count by using:

    { data: "permission.length" }
    

    permission is an array of data (in this case you could just have it with an id property only - since you aren't using anything else for it - e.g. labels like I have in the Mjoin example).

    That said, it is possible to execute SQL functions such as COUNT as a field instance by using:

    Field::inst('assets')
      ->dbField('COUNT( ... )')
      ->set(false) // not writeable
    

    Regards,
    Allan

  • rob1strob1st Posts: 84Questions: 22Answers: 0

    Awesome, thanks for taking the time to clarify Allan.

Sign In or Register to comment.