SELECT * FROM mytable
WHERE ('myfield1' = 'someval1' AND 'myfield2' = 'someval1')
   OR ('myfield1' = 'someval2' AND 'myfield2' = 'someval2')
   OR ('myfield1' = 'someval3' AND 'myfield2' = 'someval3')

I'm trying to do this with a foreach() loop:

foreach($mywords as $w)
    ->condition('myfield1', $w)
    ->condition('myfield2', $w);

Each iteration of the loop, however, should add the next set of conditions.

  • Haven't worked much with drupal 7, but can you pass an array to condition? If not you should be able to add the conditions like you are in the example. I dont see why not. – Matt Jul 29 '11 at 3:25
  • also try do print (string)$myquery; to see what it's creating. It looks like drupal has a built-in toString method to convert it to the actual query. – Matt Jul 29 '11 at 3:34
  • I checked the toString and the above produces AND's instead of OR's where I need them – user1750 Jul 29 '11 at 3:55
  • Try db_or()->condition('myfield1', $w)->condition('myfield2', $w); – Matt Jul 29 '11 at 4:05
  • Actually you may have to do this $myquery->db_or()->condition(condition('myfield1', $w)->condition('myfield2', $w)); It's a condition inside a condition. – Matt Jul 29 '11 at 4:13

You need to do something like this:

$query = db_select('mytable', t)->fields('t');
$condition = db_or();
foreach($mywords as $w {
  $condition->condition(db_and()->condition('myfield1', $w)->condition('myfield2', $w));
$result = $query->condition($condition)->execute();

This is not tested but should work.

For reference, see db_or and db_and

  • thanks again! How did you learn so much about Drupal? (serious question) – user1750 Jul 29 '11 at 20:13
  • @user1750 It's my work and my hobby. I spend a lot of time working with Drupal one way or the other. – googletorp Jul 29 '11 at 20:25

The conditions you add in that way are added using and "AND" operator; see the code of the constructor of the SelectQuery() class, and SelectQuery::condition

  public function __construct($table, $alias = NULL, DatabaseConnection $connection, $options = array()) {
    $options['return'] = Database::RETURN_STATEMENT;
    parent::__construct($connection, $options);
    $this->where = new DatabaseCondition('AND');
    $this->having = new DatabaseCondition('AND');
    $this->addJoin(NULL, $table, $alias);

  public function condition($field, $value = NULL, $operator = NULL) {
    $this->where->condition($field, $value, $operator);
    return $this;

This means you need to call myquery->condition() only once, or the resulting query will use "AND" between the conditions. SelectQuery::condition() accepts also an instance of DatabaseCondition, such as one returned from db_or() and db_and(); both the functions are helpers build on the constructor of DatabaseCondition.

function db_and() {
  return new DatabaseCondition('AND');

function db_or() {
  return new DatabaseCondition('OR');

The code should then build a DatabaseCondition object, and pass it to myquery->condition().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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