Get only 1 of each journey

Asked

Viewed 62 times

1

Well, I have a table called tabelajornadas.

In it, I have several records, and in which I have the column jornada, who’s kind int.

In it, they contain several records with the same number in the journey column.

What I intend to do is a select, that gives me only 1 record of each journey, in ascending order of journey.

How can I do that?

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

3 answers

0

As you have not described how is modeled the table(s) you want help from, for you to select only one record with the same journey number do:

SELECT param1, param2, param3 FROM myTable WHERE jornada = "minhaJornada" ORDER BY param1 ASC LIMIT 1

This way, you will be selecting param1, param2, param3 table myTable where the column journey is equal to a specified journey - what is being ordered in ascending order by param1 and limiting the search to only one outworking - exactly as described in your doubt.

  • This is not working, what I want is 1 result of each journey. This is only showing me 1 result of the first journey.

  • Edit your question and please put how your table is mounted. Based on this, I will edit my answer.

  • And based on Ou seja se na tabela, tenho 50 registos, com o mesmo numero de jornada, quero que me mostre apenas 1. - my answer is correct.

  • Okay, I’ve edited the question.

0

You can use the clause NOT EXISTS to check if there are codes larger than the current one for the specific journey:

SELECT tj.*
  FROM tabelajornadas tj
 WHERE NOT EXISTS(SELECT 1
                    FROM tabelajornadas tj2
                   WHERE tj2.jornada = tj.jornada
                     AND tj2.codigo < tj.codigo)

0

What you need is to group the records. Then you can use the clause GROUP BY. Here is an example based on your schema:

SELECT
    jornada
FROM
    tabelajornadas
GROUP BY
    jornada
ORDER BY
    jornada;

The GROUP BY groups the result by the columns you report. You can read more about it here: https://www.w3schools.com/sql/sql_groupby.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.