1

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) ))
5
  • 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
    Commented Oct 24, 2013 at 10:44
  • @Mołot yes it is , I made it working seeing the original :), thanks
    – arpitr
    Commented Oct 24, 2013 at 13:38
  • @Clive true, I will keep that in mind
    – arpitr
    Commented Oct 24, 2013 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
    Commented Oct 24, 2013 at 13:41
  • @Mołot I was doing the same little late :)
    – arpitr
    Commented Oct 24, 2013 at 13:42

2 Answers 2

4

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.

1
  • 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
    Commented Oct 24, 2013 at 10:54
2

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) )))
1
  • inequalities or condition match was neither my concern nor I have added in the code above
    – arpitr
    Commented Oct 24, 2013 at 14:00

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