Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
¿Cómo puedo seleccionar las penúltimas filas en una tabla mysql, agrupadas por columna?

La estructura es:

 CREATE TABLE current ( id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), symbol VARCHAR(5), UNIQUE (id), INDEX (symbol) ) ENGINE MyISAM;
identificación símbolo
1 A
2 B
3 C
4 C
5 B
6 A
7 C
8 C
9 A
10 B

estoy usando lo siguiente

 SELECT * FROM current WHERE id IN ( SELECT MAX(id) FROM current GROUP BY symbol )

para devolver los últimos registros de una tabla.

identificación símbolo
8 C
9 A
10 B

¿Cómo puedo devolver los penúltimos resultados de manera similar?

se que necesito

 ORDER BY id DESC LIMIT 1,1

en algún lugar, pero mi foo es débil.

yo quisiera volver

identificación símbolo
5 B
6 A
7 C
over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

SELECT * FROM current WHERE id IN ( SELECT DISTINCT T.id FROM current AS T WHERE id=( SELECT id FROM current WHERE symbol=T.symbol ORDER BY id DESC LIMIT 1,1 ) )
over 4 years ago · Santiago Trujillo Report

0

Fácil si su MySql puede usar ROW_NUMBER. (MySql 8)
Solo hazlo descender, luego toma el segundo.

 WITH CTE AS ( SELECT * , ROW_NUMBER() OVER (PARTITION BY symbol ORDER BY id DESC) AS symbol_rn FROM current ) SELECT id, symbol FROM CTE WHERE symbol_rn = 2 ORDER BY id;

En MySql 7.5 puede simplemente unirse en el símbolo y agrupar por.
Luego, el penúltimo tendrá 1 ID más alto.

 SELECT c1.id, c1.symbol FROM current c1 LEFT JOIN current c2 ON c2.symbol = c1.symbol AND c2.id >= c1.id GROUP BY c1.id, c1.symbol HAVING COUNT(c2.id) = 2 ORDER BY c1.id; 
identificación símbolo
5 B
6 A
7 C

db<>violín aquí

El rendimiento realmente se beneficiará de un índice en el símbolo.

over 4 years ago · Santiago Trujillo Report

0

Para versiones de MySql anteriores a la 8.0, use una subconsulta en la cláusula WHERE para filtrar la id máxima de cada símbolo y luego agregue:

 SELECT MAX(id) id, symbol FROM current WHERE id NOT IN (SELECT MAX(id) FROM current GROUP BY symbol) GROUP BY symbol ORDER BY id;

Ver la demostración .

over 4 years ago · Santiago Trujillo Report

0

Puedes probar esto;

 SELECT * FROM current WHERE id IN (SELECT MAX(id) FROM current GROUP BY symbol) ORDER BY id DESC LIMIT 1,3

límite 1,3 dice; obtener los últimos 3 resultados excluyendo el último resultado. Puedes cambiar los números.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!