Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@SeanJA
Created July 3, 2012 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeanJA/3041858 to your computer and use it in GitHub Desktop.
Save SeanJA/3041858 to your computer and use it in GitHub Desktop.
drupal equivalent
SELECT COUNT(*)
FROM table_1
JOIN table_2 ON (table_1.id=table_2.table_1_id)
WHERE table_1.column != 'foo'
<?php
//query_builder
$q = new query($db);
$q->column('COUNT(*)')
->table('table_1')
->join('table_2', 'table_1.id=table_2.table_1_id')
//query::NOT_EQUAL === '!='
->where('table_1.column', 'foo', query::NOT_EQUAL);
//drupal 7 version
//why is this so complicated and somewhat inconsistent?
//set the initial table
$query = db_select('table_1', 't1');
//returns the table alias (t2 in this case) ?
//breaking the chain
$query->innerJoin('table_2', 't2', 't1.id=t2.table_1_id');
//continue building the query (this part is chainable again)
$query->fields('', 'COUNT(*)')
->condition('table_1.column', "foo", '!=');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment