Database Class: GROUP BY

Database Class: GROUP BY

ComGreedComGreed Posts: 15Questions: 3Answers: 1

I'm currently rewriting everything in my project from medoo to Datatables. The only query that is left is the following:
$medoo->select('form_autocomplete', ['type', 'text' => Medoo::raw('GROUP_CONCAT(text)')], ['GROUP' => 'type']);
What I have so far is:
$db->query('select', 'form_autocomplete')->get(['type', 'GROUP_CONCAT(text) as text'])->exec()->fetchAll();

I already checked the documentation and the source code but there is unfortunately no ->group_by('type'). Does anyone have an idea how I could do that. I know that you can extend classes in PHP but I don't think that this is all that needs to be done there.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin
    Answer ✓

    You could use the sql method which just takes a raw query string and executes it without trying to do anything clever (building the SQL).

    $db->sql('SELECT type, GROUP_CONCAT(text) as text FROM form_autocomplete GROUP BY ...')->fetchAll()
    

    Its not ideal, but it does allow SQL methods to be executed for which the libraries don't have an abstraction (such as GROUP BY).

    Allan

  • ComGreedComGreed Posts: 15Questions: 3Answers: 1
    edited November 2019

    If I had known about this method I would have used it right away. This solution is more then enough.

    Thanks a lot.

This discussion has been closed.