Display grouped data

0

I am doing a query in the database and am having difficulties with data duplication, 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:

The first table is tec_postagens , the second is tec_historico_status , I need to show all the posts along with the data of tec_historico_status Both have the same protocolo What sets is datahora I need to show in order of datahora

Thank you.

    
asked by anonymous 12.11.2014 / 13:50

2 answers

3

If you do not want to repeat in the postagem column, you must 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 sort by datahora of the second table, just change

ORDER BY tp.protocolo DESC, tp.datahora DESC

by

ORDER BY tp.protocolo DESC, th.datahora DESC

Notice that I changed tp.datahora by th.datahora

    
12.11.2014 / 16:31
0

Try 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 does not work, try changing * to column names.

    
12.11.2014 / 15:31