-1
I have two tables that I am joining to recover the related data in this way:
Tabela Processos: Tabela Anexos
id | processos processoid | titulo
1 | Processo 1 1 | REQUERIMENTO
2 | Processo 2 1 | Apostilamento
3 | Processo 3 2 | requisição
4 | Processo 4 2 | REQUERIMENTO
5 | Processo 5 3 | Apostilamento
6 | Processo 6 3 | CRAF
SELECT processos.processos,
processos.id as processoid,
anexos.*,
anexos.id as anexoid
FROM anexos
INNER JOIN processos ON (processos.id = anexos.processoid)
GROUP By processos.id
ORDER By anexos.processoid
With the above query it returns as follows:
Processo 1 => REQUERIMENTO
Processo 1 => Apostilamento
Processo 2 => requisição
Processo 2 => REQUERIMENTO
Processo 3 => Apostilamento
Processo 3 => CRAF
What I need is below:
Processo 1 => REQUERIMENTO
Apostilamento
Processo 2 => requisição
REQUERIMENTO
Processo 3 => Apostilamento
CRAF
I believe it is only the use of GROUP BY but in this case it does not work.
Why doesn’t the first result meet you?
– Glenys Mitchell
Hello, do not answer me because I want to display the value of the grouped and non-repeated process.
– José Roberto Juliana
It’s not simple to do that, you’d have to put a view or a program together to do that, and they’re very complex to program. A quick solution to the problem would be you program your php to present the way you want.
– Glenys Mitchell
so buddy, I know you have to manipulate the query, in this case I think it has to do with GROUP By because I need to return the process field 1 time for each title of the Attachments table, in this case it repeats the process field.
– José Roberto Juliana
Group By does not solve Cartesian plan, in your case is returning the index twice because the results of the second column is different.
– Glenys Mitchell
would doing two separate query resolve?
– José Roberto Juliana
It will always bring two results?
– Glenys Mitchell
no, it’s random, some 3 other 5...
– José Roberto Juliana
That complicates it. I insist that you can do it with php. You want to present this in a certain table?
– Glenys Mitchell
I’ll list the documents of each process in relation to the title, maybe use table but I think I’ll do something more intuitive.
– José Roberto Juliana
You bring the database result into array or object ?
– Glenys Mitchell
I bring in array, do a foreach and display the processes and titles.
– José Roberto Juliana
That return is in that language?
– adventistaam
return is PHP direct.
– José Roberto Juliana
Then the names of the processes would be the keys, correct?
– adventistaam
The way you want it, just to do it in the language, in this case php
– adventistaam
Exactly, and I would have him mount screen through conditions as he traverses the loop.
– Glenys Mitchell
This should be done in your application and not in the SQL query since this "repetition" is what is expected from the query result. The use of some report Generator makes it easier to do what you want.
– anonimo