Edit return the COUNT of a column

Edit return the COUNT of a column

mauro218mauro218 Posts: 9Questions: 5Answers: 0

I'm trying to return the count of ocurrences from a Left join table.

Entity table:
idEntity        EntityName
1                  ABC
2                  XYZ
Qualification table:
idEntity        State
1                  Florida
1                  California
2                  New York
Expected Result:
idEntity        EntityName        Total
1                  ABC                  2
2                  XYZ                  1
$editor = Editor::inst( $db, 'Entity', 'idEntity' )
    ->fields(
        Field::inst( 'Entity.idEntity' ),
        Field::inst( 'Entity.EntityName' ),
        Field::inst( 'COUNT(Qualification.idEntity) as Entity.Total' )
    )
    ->leftJoin( 'Qualification', 'Qualification.idEntity', '=', 'Entity.idEntity' );

However, it's returning only the first record with Total = 3

What am I doing wrong?

Thanks for the help

Jhonny

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi Jhonny,

    Unfortunately, at the moment the Editor fields cannot accept SQL methods as parameter names, which is why this won't work. Longer term it is something I would like to add, but at the moment what you will need to do is use an array join and use the length property of the resulting array.

    Allan

  • mauro218mauro218 Posts: 9Questions: 5Answers: 0

    Allan,

    Is this the right way to get the array join? It's not returning anything at all

    $editor = Editor::inst( $db, 'Entity', 'idEntity' )
        ->fields(
            Field::inst( 'Entity.idEntity' ),
            Field::inst( 'Entity.EntityName' )
        )
        ->join(
            Join::inst( 'Qualification', 'array' )
                ->join( 'idEntity', 'idEntity' )
                ->fields(
                    Field::inst( 'idEntity' ),
                    Field::inst( 'FilingNumber' )
                )
        );
    

    Jhonny

  • mauro218mauro218 Posts: 9Questions: 5Answers: 0

    Nevermind Allan, it worked!!! Thanks!

This discussion has been closed.