I am looking to achieve GROUP BY CAST(date_time AS DATE)
in a db_select
function in Drupal 7 but with no idea how.
The function up until now is:
foreach ($distinct_users as $user){
$day_record = db_select('mytable', 'p')
->condition('p.processed', 0, '=')
->condition('p.first_name', $user['first_name'], '=')
->condition('p.last_name', $user['last_name'], '=')
and I need to group the results by day which is in Y-m-d H:i:s
in my table column.
Please advise.
$connection = Database::getConnection(); $sth = $connection->select('mytable', 'p'); $sth->condition('p.processed', 0, '=') ->condition('p.first_name', $user['first_name'], '=') ->condition('p.last_name', $user['last_name'], '='); $sth->addExpression('CAST(p.date_time)', 'cast'); $sth->groupBy('p.date_time'); // Execute the statement $data = $sth->execute(); // Get all the results $results = $data->fetchAll(\PDO::FETCH_ASSOC);
see here drupal.org/forum/support/module-development-and-code-questions/…