A query to add the values and select all fields according to the clause "Where" and "or" with the like operator

Asked

Viewed 80 times

0

In cakephp, I want to do a query that takes all the values according to the parameter passed to the expense field or to the data_expense field, as well as summing the values of the expense value_field. It would look something like this:

$this->Despesa->query("select sum(valor_despesa) select from despesas where despesa like '%escola%' or data_despesa like '%2015%'");  

I only managed to solve it by making two querys. One to find and add and the other to find all table columns according to the parameters passed. I want everything, if possible, in a single query.

  • Could you explain a little more? vc want the same total on all lines?

  • Does this SQL work? 'select sum(value_expense) select ...' ?

  • @Romario Pires For example. I have 10 expenses in the bank. According to the parameters passed in Where expense like '%keyword%' or data_expense like '%2015-11-15%', if I find 5 records among the 10 totals, I want to add up the total cost of the 5 expenses found. Each individual expense has its cost, I want to add it to the 5. The query select * from expenses with the quoted clauses works. And select sun(expense) from expenses also works, but I want it all in one query, like two select in one dml.

  • @Rui Costa tried to use the two instructions together, but they don’t work, just succeed with the separate querys. I saw an example that worked. I just can’t remember where. I think it was here at stackoverflow.

  • @Sorry for what I’m suggesting: select sum(value_expense) from expenses Where expenditure like '%school%' or data_expense like '%2015%' This is not what you want?

  • @Rui Costa was clear on the question. I want a query with two commands. Reread the question.

  • @Andrénascimento Calma André we are here to try to help you.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.