1

How do I use GROUP BY to display the result by group (email)? this is my file .php

<?php
/**
 * Created by PhpStorm.
 */
namespace Drupal\gestion_contact\Controller;
use Drupal\Core\Controller\ControllerBase;
class GestionContactController extends ControllerBase
{
    public function admin() {


        $result = db_query("SELECT * FROM {contact}");

            $node = $result->fetchAll();

        return array(
            '#theme' => 'gestion_contact',
            '#nodes'=> $node,

        );
    }
    public function delete($id)
    {

        db_delete('contact')
            ->condition('id_contact', $id)
            ->execute();
        drupal_set_message(t('Your form has been deleted'));

        $result = db_query("SELECT * FROM {contact}");

        $node = $result->fetchAll();
        return array(
            '#theme' => 'gestion_contact',
            '#nodes' => $node,

        );
    }
    public function afficher()
    {

        $result = db_query("SELECT * FROM {contact} WHERE vu = :vu",
            array(':vu' => 'false'));
        $node = $result->fetchAll();
        return array(
            '#theme' => 'nouveaux_contact',
            '#nodes' => $node,
        );
    }
}

this is a log messages this is my table contact

7
  • Look at second line ...BY: SELECT * FROM..., this is causing error. Look at Edit 1 on my answer, by default Drupal use sql_mode with 'only_full_group_by', this means that you cannot use a aggregate function (GROUP BY) with non aggregated column. In this Edit also provide an example to solve this.
    – Vagner
    Commented May 10, 2016 at 14:41
  • I did not understand, what is the problem exactly? how to solve it? can you write me an example?
    – Sony
    Commented May 10, 2016 at 15:17
  • Use SELECT email instead of SELECT *, if you only need email column.
    – Vagner
    Commented May 10, 2016 at 15:30
  • I need all the column, how to do aggregation with GROUP BY?
    – Sony
    Commented May 10, 2016 at 15:45
  • @Vagner I am waiting for your answer
    – Sony
    Commented May 10, 2016 at 23:31

1 Answer 1

3

Follow an example:

$result = db_query('SELECT tid, count(tid) AS count FROM {taxonomy_term_field_data} WHERE vid <> :vid GROUP BY tid', array(':vid' => 'tags'));

But, from docs...

Deprecated

as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call query() on it. For example, $injected_database->query($query, $args, $options);

More info in db_query.

Edit 1:

Drupal set by default sql_mode with 'only_full_group_by', in your case when you use SELECT *, an database exception is throw, look in your log, you will see something like this:

Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.table.field' which is not functionally dependent on columns in GROUP BY clause.

So to correct this you must aggregate all fields in your SELECT list. If you only need e-mail list something like this can solve:

db_query('SELECT email FROM {contact} GROUP BY email');
4
  • I created this code but i doesn't work "The website encountered an unexpected error. Please try again later." $result = db_query("SELECT * FROM {contact} GROUP BY Email "); $node = $result->fetchAll();
    – Sony
    Commented May 10, 2016 at 11:17
  • Check your db if you have this table {contact} with this field email. Run this select in your preferred app and see the results.
    – Vagner
    Commented May 10, 2016 at 11:32
  • yes, I have this table and field , when I run the instruction without group by,it working properly
    – Sony
    Commented May 10, 2016 at 12:07
  • @Sony added more information.
    – Vagner
    Commented May 10, 2016 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.