gugoan, as I said in the comment, if Mysql would support CTE, the query would be much simpler, in any case I would like that in your next question about SQL, you prepare a Sqlfiddle like this: http://sqlfiddle.com/#! 9/add59/1
Node that its categories were a tree with only two levels, so all eyes are directly linked to the root, in this case a simple LEFT JOIN
with COALESCE
should work.
SELECT
id,
desc_cat,
SUM(value)
FROM (
SELECT
COALESCE(CatParent.id, Category.id) id,
COALESCE(CatParent.desc_cat, Category.desc_cat) desc_cat,
CashBook.value
FROM CashBook
JOIN Category ON CashBook.category_id = Category.id
LEFT JOIN Category AS CatParent ON Category.parent_id = CatParent.id
) AS CatParent
GROUP BY id, desc_cat
But this approach will only work in this situation, if your tree goes deeper, you will have problems... for example... if your table Category
has the following data:
| id | desc_cat | parent_id |
|----|---------------------|-----------|
| 26 | Categoria 1 | (null) |
| 27 | Sub Categoria 1.1 | 26 |
| 28 | Sub Categoria 1.1.1 | 27 |
| 29 | Sub Categoria 1.1.2 | 27 |
| 30 | Sub Categoria 1.2 | 26 |
| 31 | Sub Categoria 1.2.1 | 30 |
| 32 | Sub Categoria 1.2.2 | 30 |
I even tried to emulate a recursive query using CTE
using View
or a Function
with MySQL
... but I couldn’t do it in the SQLFiddle
, I believe that the MySQL
does not support this type of structure.
You can still use one WHILE
to try to do something but its MySQL
you will not be able to infer an execution plan, your query will become complex. I would only advise you to do this for a scheduled JOB.
However as this data will be consumed in PHP, the ideal is to do this type of direct control in PHP... unfortunately I do not know enough PHP to give an example, but I can update this answer with a snippet of Code in C# or Javascript, then you could make a port from this logica to PHP.
Welcome to Mysql, the big white elephant, the only big DBMS that doesn’t support the Function window and common table Expressions
– Tobias Mesquita
Right. In this case, I need to know if there is a simple way to do it, because I’m using a shared hosting and I don’t have advanced access to the comic. Know a way @Tobymosque ?
– gugoan
These values that you want to put in the father, are where ? Which table and in which column of this table ? And when you talk about the sum of values, you want to add up what values ? .
– Miguel Soeiro
@Sound where it is stored as
null
, means it is a parent record, otherwise it would contain some ID of some other parent record– gugoan
@gugoan My question is in the example table you have put, in the row corresponding to "Personal" has a total of 560.55 and this number is composed by a sum of values of the table "Cashbook", but which fields are added ? Briefly, what is the field you want to find out and where are the data so that they can be added up to get the value? And what are they? I also suggest that you review some conseitos about mysql, which are mother tables, column, row, table and etc, will help you when asking questions and they will be better understood, if they are put in the correct terms. I hope to help
– Miguel Soeiro