What is the corresponding alternative in Drupal 7 for the following query
SELECT row_id,name,email, COUNT(name) AS name_count
FROM tn_api_users
GROUP BY name
HAVING (name_count > 1)
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThat would need a combination of SelectQuery::addExpression()
, and SelectQuery::having()
, e.g.
$query = db_select('tn_api_users', 'u')
->fields('u', array('row_id', 'name', 'email'))
->groupBy('name')
->having('name_count > 1');
$query->addExpression('COUNT(name)', 'name_count');
Output:
SELECT u.row_id AS row_id, u.name AS name, u.email AS email, COUNT(name) AS name_count
FROM
{tn_api_users} u
GROUP BY name
HAVING (name_count > 1)
->groupBy('row_id')
to the chain before the name
group by? That would probably ruin your counting though. It's going to be tricky to get individual results if you're grouping the query on that column
code
->groupBy('row_id')code
didn't work out. I will just create an array and it will be two-step process. Thanks again !
Oct 19, 2013 at 13:58