View grouped data

Asked

Viewed 289 times

0

I am making a query in the database and I am having difficulties with data duplications, so far, this is my query:

SELECT * FROM tec_postagens tp
INNER JOIN tec_historico_status th ON tp.protocolo=th.protocolo
WHERE tp.protocolo = '1168'
ORDER BY tp.protocolo DESC, tp.datahora DESC

the result is :

inserir a descrição da imagem aqui

the first table is tec_postagens, the second is tec_historico_status, I need to show all posts along with the data from tec_historico_status both have the same protocolo what differentiates is the datahora I need to show by order of datahora

thank you.

  • 2

    In my view it is not wrong. Because you are linking everything in a single query. These duplicities must be adjusted using the programming language in their application.

  • You just don’t want duplicate data to appear? Can’t use DISTINCT?

  • never used DISTINCT @Renesá , could help me?

  • 1

    Thus: SELECT DISTINCT * FROM tec_postagens tp
INNER JOIN tec_historico_status th ON tp.protocolo=th.protocolo
WHERE tp.protocolo = '1168'
ORDER BY tp.protocolo DESC, tp.datahora DESC With DISTINCT, if there are multiple identical results, evaluating the sequence by ORDER BY, it returns only one. @Rene, put the solution as answer.

  • was the same result @Gustavocinque

  • Furlan where there’s repetition, I don’t understand...

  • in the column posting @Jorgeb.

  • Try to explain your problem better in the question. It is not possible to realize what you want.

Show 3 more comments

2 answers

3


If you don’t want repetition in the column postagem has to make a GROUP BY of that column:

SELECT * FROM tec_postagens tp
INNER JOIN tec_historico_status th ON tp.protocolo=th.protocolo
WHERE tp.protocolo = '1168'
ORDER BY tp.protocolo DESC, tp.datahora DESC
GROUP BY tp.postagem

[EDIT]

If it’s only to order by datahora of the second table just change

ORDER BY tp.protocolo DESC, tp.datahora DESC

for

ORDER BY tp.protocolo DESC, th.datahora DESC

Notice that I’ve changed tp.datahora for th.datahora

  • right @Jorge but now I miss some 'changes' he loses because of GROUP

  • @Furlan Which of the lines in the image you want NOT to appear?

  • repeated posts cannot appear, and cannot lose the changes

  • Would you take a whole row from there? Tell me the number of the line you want not to appear from that table in the image

  • do not have to take anything, I want to show only the posts and changes according to the time of each one, ie listing them ordered by the column 'date time'

  • Look at my edition... that’s it?

  • Valew guys for help, I managed to solve my mistake, I changed my structure, thank you all, Valew @Jorge

  • 1

    @Jorgeb. Great example +1

Show 3 more comments

0

Try it this way, using DISTINCT.

SELECT DISTINCT * FROM tec_postagens tp 
INNER JOIN tec_historico_status th ON tp.protocolo=th.protocolo 
WHERE tp.protocolo = '1168' 
ORDER BY tp.protocolo DESC, tp.datahora DESC

If that doesn’t work, try changing the * by the names of the columns.

  • ta displaying, just what type, the column posting ta repeating the values, I needed to display the posts and changes according to the time, post 2014-11-11 10:00:00, change 2014-11-11 11:00 and so on understood

  • I get it, but how are they displayed? because in the Voce query it asks to be displayed decreasingly

Browser other questions tagged

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