How to select the second and the penultimate item in php?

2

How can I select the second and the last item in a MySQL table with php.

I tried to do the following but limited myself to position 12 of 15, and must show me from position 2 to 14 of 15 in total, the second and the penultimate item.

The idea is that if more positions are added, the penultimate will always be shown, in case a new one is added 16 and the penultimate would already be 15. and so with each new record.

My query

$query_paradas = "SELECT pos, r_lat, r_lng FROM escolar WHERE 
placa='$placa_id' AND r_ruta='$ruta' AND jornada='$jornada' AND 
estado='activado' AND pos  BETWEEN 2 and 12 ORDER BY pos ASC";
$paradas = mysqli_query( $con, $query_paradas );

Thank you very much for the help

    
asked by Franck 30.08.2018 в 04:05
source

1 answer

4

Let's see, let's see your problem in another way ...

You want the second item, which will always be pos = 2 , and also the last one, which will always be select count(*) - 1 from escolar ... then your query is much simpler than you think, nor bring all, or bring by parts or anything ...

SELECT pos, r_lat, r_lng 
FROM escolar 
WHERE placa='$placa_id' AND r_ruta='$ruta' AND jornada='$jornada' AND 
estado='activado' AND (pos  =2 or pos = (select count(*)-1 from escolar))

Note:

I'm not sure if mysql would accept the following syntax, but it also seems like a good idea:

SELECT pos, r_lat, r_lng 
FROM escolar 
WHERE placa='$placa_id' AND r_ruta='$ruta' AND jornada='$jornada' AND 
estado='activado' AND pos in (2,(select count(*)-1 from escolar))
    
answered by 30.08.2018 / 04:34
source