Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
MySQL join table like concat but only pick latest row

I have case where I need latest status. There are two tables, I simplify my table so it looks like below:


Table A
+------------+
|  a_id      |
|------------|
|  A1        |
|  A2        |
+------------+

Table B
+------------+-------------+------------------+------------------+
|  b_id      |  a_id       |  status          |  created_at      |
|------------+-------------+------------------+------------------|
|  B01       |  A1         |  something       |  2020-03-14      |
|  B02       |  A1         |  something else  |  2020-04-15      |
|  B03       |  A2         |  anything        |  2020-03-22      |
+------------+-------------+------------------+------------------+

I want to show table from A with join table B so it will show like this:


+------------+--------------------+-----------------+
|  a.a_id    |  b.status          |  b.created_at   |
|------------+--------------------+-----------------|
|  A1        |  something else    |  2020-04-15     |
|  A2        |  anything          |  2020-03-22     |
+------------+--------------------+-----------------+

I will appreciate Mysql query or codeigniter query builder. Thank you

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you're running MySQL 8.0, you can do this with row_number() :

 select a.a_id, b.status, b.created_at from tablea a inner join ( select b.*, row_number() over(partition by a_id order by created_at desc) rn from tableb b ) b on a.a_id = b.a_id and b.rn = 1

In older versions, one option is to filter with a correlated subquery:

 select a.a_id, b.status, b.created_at from tablea a inner join tableb b on a.a_id = b.a_id where b.created_at = ( select max(b1.created_at) from tableb b1 where b1.a_id = b.a_id )

For performance with the correlated subquery solution, consider an index on tableb(a_id, created_at) .

over 4 years ago · Santiago Trujillo Relatório

0

  1. First find the last created_at value from table B

Example: SELECT MAX(created_at) FROM B GROUP BY a_id;

  1. Then use the first result in the query below and you will get the desired result.

     SELECT FROM A JOIN B ON A.a_id = B.a_id WHERE B.created_at IN (SELECT MAX(created_at) FROM B GROUP BY a_id);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda