1

This question already has an answer here:

How can I convert the following query with db_select

SELECT *
FROM 'my_table'
WHERE
(condition1 < "value1" AND ((conidition2 > "value2") OR (condition3 < "value3")))
  OR
(condition4 < "value5" AND ((condition6 < "value6") OR (condition7 < "value7")))

I am trying with

$query = db_select('mytable');

$query->condition(
          db_and()->condition("condition1","value1")
                 ->condition(
                             db_or()->condition("condition2","value2")->condition("condition3","value3")
                             ));
$query->condition(
          db_and()->condition("condition4","value4")
                 ->condition(
                             db_or()->condition("condition5","value5")->condition("condition6","value6")
                             ));

  $query_debugg = $query->__toString();
dpm($query_debugg);

issue is with the "AND" in between where is should be "OR", it is expected I know.how can I change it

Output String

SELECT 
FROM 
{mytable} mytable
WHERE ( (condition1 = :db_condition_placeholder_0) AND( (condition2 = :db_condition_placeholder_1) OR (condition3 = :db_condition_placeholder_2) ))AND( (condition4 = :db_condition_placeholder_3) AND( (condition5 = :db_condition_placeholder_4) OR (condition6 = :db_condition_placeholder_5) ))

marked as duplicate by Mołot, Free Radical, monymirza, 4life, AjitS Oct 25 '13 at 5:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Can you post the code you've already tried, and the query string that returns please? At the moment you're just asking for someone to do your work for you, which isn't an appropriate use of the site (it's a shortcut to getting your question closed on pretty much any Stack Exchange site). You've put db-or as a tag so you definitely know where to start, just edit the question with details of that code, and the errors you get, and I'm sure you'll get a good answer – Clive Oct 24 '13 at 10:44
  • @Mołot yes it is , I made it working seeing the original :), thanks – arpitr Oct 24 '13 at 13:38
  • @Clive true, I will keep that in mind – arpitr Oct 24 '13 at 13:38
  • @arpitr if you solved it, you can post a self-answer, can't you? But I think it's a duplicate anyway. Un-downvoted after seeing you actually did put some effort before asking. – Mołot Oct 24 '13 at 13:41
  • @Mołot I was doing the same little late :) – arpitr Oct 24 '13 at 13:42
3

In Drupal, conditions are AND by default. It's easier to use objects directly, in my opinion:

$left_condition = new DatabaseCondition('AND');
$left_condition->condition( ... ); // add what you need

$right_condition = new DatabaseCondition('AND');
$right_condition->condition( ... ); // add what you need

$condition = new DatabaseCondition('OR');
$condition->condition($left_condition);
$condition->condition($right_condition);

But you can use db_and() and db_or() if you prefer.

  • Posted an answer as this question appeared high in my Google results for related queries (so fast?! Bad Google!), to prevent disappointed googlers. – Mołot Oct 24 '13 at 10:54
1

with help of question pointed by @molot I could make it work, here is the code

$query = db_select('mytable');
$db_or = db_or();
$db_or->condition(
  db_and()
    ->condition("condition1","value1")
    ->condition(
      db_or()
        ->condition("condition2","value2")
        ->condition("condition3","value3")
));
$db_or->condition(
  db_and()
    ->condition("condition4","value4")
    ->condition(
      db_or()
        ->condition("condition5","value5")
        ->condition("condition6","value6")
));
$query->condition($db_or);

$src = $query->__toString();
dpm($src);

and here is the output

SELECT 
FROM 
{mytable} mytable
WHERE (( (condition1 = :db_condition_placeholder_0) AND( (condition2 = :db_condition_placeholder_1) OR (condition3 = :db_condition_placeholder_2) ))OR( (condition4 = :db_condition_placeholder_3) AND( (condition5 = :db_condition_placeholder_4) OR (condition6 = :db_condition_placeholder_5) )))
  • inequalities or condition match was neither my concern nor I have added in the code above – arpitr Oct 24 '13 at 14:00

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