How can I get the first value of several related tables?

2
SELECT pr.nombre AS 'nombrePrograma',pr.id 
FROM programas pr
JOIN cursos ON pr.id = cursos.programa_id 
LIMIT 1 
    
asked by hostLondoño empezar 01.11.2018 в 17:47
source

1 answer

0

The query that you sample is correct but incomplete, you should try this way

SELECT pr.nombre AS 'nombrePrograma',pr.id 
FROM programas pr
JOIN cursos ON pr.id = cursos.programa_id 
ORDER BY pr.nombre DESC
LIMIT 1

By using ORDER BY you are indicating that from the results dataset you order it by the column pr.nombre and indicating it so DESC will sort it in this way

If I have these records

Nombre
______
Alfredo
Daniel
Julio
Pedro
Beto

THE RESULT SHOULD BE LIKE THIS

Nombre
______
Alfredo
Beto
Daniel
Julio
Pedro

Now if you want the order to be made from the most recent record, then use the column that you have registered by id; I would do it this way

SELECT pr.nombre AS 'nombrePrograma',pr.id 
FROM programas pr
JOIN cursos ON pr.id = cursos.programa_id 
ORDER BY pr.id DESC
LIMIT 1
  

When handling sorting by the column% co_of% that is or should be   a primary key will return the last record made and the   combine it with id will only show a final record

    
answered by 01.11.2018 в 18:06