0

What is the correct way to do what I'm trying? Discover that the checks IN does what I need.

$hierarquia = implode(",", $_POST['hierarquia']);
// Show: 3,4

(
    SELECT planoDeConta FROM `financ_receita` 
    WHERE data BETWEEN '2011-01-01' AND '2013-12-30'
    planoDeConta IN ($hierarquia)

) UNION ALL (

    SELECT planoDeConta FROM `financ_despesa` 
    WHERE data BETWEEN '2011-01-01' AND '2013-12-30'
    planoDeConta IN ($hierarquia)

)
1
  • 1
    What makes you think your way is incorrect? Commented Dec 30, 2013 at 12:57

2 Answers 2

1

You have a syntax error because you are missing a conjunction before the second condition in each query:

(
    SELECT planoDeConta
    FROM `financ_receita` 
    WHERE data BETWEEN '2011-01-01' AND '2013-12-30' AND
          planoDeConta IN ($hierarquia)

) UNION ALL (
    SELECT planoDeConta
    FROM `financ_despesa` 
    WHERE data BETWEEN '2011-01-01' AND '2013-12-30' AND
          planoDeConta IN ($hierarquia)
)

This should work because you are doing the variable substitution for $hierarquia at the "query-string" level. It would not work if you just input the value as a string.

Sign up to request clarification or add additional context in comments.

Comments

0

The values ​​must be quoted. To do this, simply use the code below:

$hierarquia2 = sprintf( "'%s'", implode( "','", $_POST['hierarquia'] ) );

Thank you all for your help.

1 Comment

This is not how you quote values to use in SQL query. There are dedicated functions for it, depending on what sql extension are you using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.