Compound fields in $db->select

Compound fields in $db->select

LapointeLapointe Posts: 430Questions: 81Answers: 4

Hi
Is there a way to get composite fields like "concat('Nom',' ','Prenom')" as :

$db->selectDistinct($tbl, array('ID', concat('Nom',' ','Prenom')))

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    I haven't actually tried it, but:

    $db->selectDistinct($tbl, array('ID', "concat('Nom',' ','Prenom')"))
    

    should work.

    Allan

  • LapointeLapointe Posts: 430Questions: 81Answers: 4

    hi @allan

    Testing.

        $result = $db->selectDistinct($OptTbl, array('ID', "concat('Nom',' ','Prenom')"),
            function ( $q ) use ( $CurUsr, $withNull ) {
                $q->where('ID_Utilisateur', $CurUsr); 
                if ($withNull) $q->or_where('ID_Utilisateur', null);
            });
    

    at lib/Database/Driver/MysqlQuery.php.DataTables\Database\Driver\MysqlQuery->_exec : lineno 108
    I get the sql error message

    "$e->getMessage()"  SQLSTATE[42000]: Syntax error or access violation: 1064 Erreur de syntaxe près de 'Nom',' ','Prenom')' FROM  `clients` WHERE (`ID_Utilisateur` = '4' OR  `ID_Utilis' à la ligne 1   
    

    This seem to be because of alias setting

    concat('Nom',' ','Prenom') as concat('Nom',' ','Prenom')
    

    So using

    $db->selectDistinct($tbl, array("ID as value","concat(`Nom`, ' ', `Prenom`) as label"))
    

    works fine.
    B)
    Bob

This discussion has been closed.