SELECT with two results

Asked

Viewed 1,103 times

1

Table: PEOPLE

id - nome
1 - BEATRIZ
2 - MARIA
3 - CARLOS
4 - MARIA
5 - JÉSSICA
6 - HENRIQUE

How do I make a SELECT that gives me as a result a list of the names ordered by ID descendente, however, a given NAME first and below the continuation?

Type: first the MARIAS and below the continuation in alphabetical order and ID DESC.

SELECT * FROM PESSOAS WHERE nome='MARIA' OR nome!='MARIA' ORDER BY nome, id DESC

Thus not from error, but from query returns me the list normally ordered only by id DESC and not with the name MARIA at the top of the list.

  • nome='MARIA' OR nome!='MARIA' this is quite wrong, nor needed this WHERE, it is the same thing as nothing. But I will answer your question

  • 2
  • "MARY" has to be the first name or is it to list first all the lines that the term "MARY" is part of the name? // The name column has only one term?

  • @Cicero which bank you use?

4 answers

4

You can try to force a casein the clause ORDER BY, I already did and would look something like this:

SELECT * 
  FROM PESSOA  
 WHERE NOME = 'MARIA' OR NOME != 'MARIA' 
 ORDER BY CASE WHEN ((NOME  = 'MARIA') OR (NOME != 'MARIA')) THEN 0 ELSE 1 END, NOME, ID;

Edited

If you want only one record to be at the top (which will not happen in SQL above), I recommend that you use only one condition in ORDER BY. It would look something like this:

SELECT * 
  FROM PESSOA  
 WHERE NOME = 'MARIA' OR NOME != 'MARIA' 
 ORDER BY CASE WHEN (NOME  = 'MARIA') THEN 0 ELSE 1 END, NOME, ID;

Edited

To leave your SQL with a better result, since the current form is not a good practice, follows suggestion:

SELECT * 
  FROM PESSOA  
 WHERE NOME LIKE '%MARIA%' 
 ORDER BY CASE WHEN (NOME LIKE '%MARIA%') THEN 0 ELSE 1 END, NOME, ID;

In this way, we will return all the names that have Mary, of Ana Maria until Maria Braga, but in this way the first record of the return will obey the second clause, which is NOME, for example:

1: Ana Maria

2: Anna Maria

3: Maria Braga

And so on and so forth.

  • 1

    Interesting your solution, just one detail: all the lines will stay with 0, the second verification in WHEN cannot exist.

  • You’re right, but in case he wanted only one client to stay at the top, there must be only one condition of equality, removing the NOME != 'MARIA'. Agrees?

  • I only did it based on his example, I will edit the answer :D

  • Your clause where is practically tautological. It has the same effect of where NOME is not NULL

  • Like I said, I answered based on his SQL. I’m not here to question the logic he used, I don’t know the end result he expects using this way, I’m just trying to help him. : D But anyway, it doesn’t make sense.

  • But then you are perpetuating something bad. It would be better to explain why this is pointless, don’t you agree? Instead of perpetuating something bad?

  • What I want is for the system to return me a list by putting a result at the top and below the continuation. I could do this by creating two queries: one for MARIA and the other for names other than MARIA. Mias would be two researches and what I want is performance improvements.I’m analyzing each answer. This CASE WHEN is something new for me I’m analyzing. I use PHP.

  • @Cicero this is pure sql, does not interfere in your life in php.

  • @Jeffersonquesado, I made an issue. See what you think now. If there is any point, please adjust.

  • 1

    You didn’t explain why it was bad practice. You also didn’t explain the tautology of a == b OR a != b second propositional logic, axiom of the excluded middle. Now you’re taking ignoring the not Marias, which is not what you want, but rather prioritize the Marias

  • This is my real query: SELECT * FROM noticias WHERE bairro_id = 5 OR bairro_id != 5 ORDER BY CASE WHEN ((bairro_id = 5) OR (bairro_)id != 5)) THEN 0 ELSE 1 END, id DESC she must return me from the NEWS table first the news of the neighborhood whose ID = 5 and then continue with the other neighborhoods. I did according to your example but the result is still the same. It shows the list, but in the end sorts only by ID DESC

  • Cicero, remove the OR of the clause ORDER BY which refers to (BAIRRO_ID != 5).

  • Thanks @Lucasdesouzacruz settled here. Thank you all for your help.

  • Mark the answer as useful to help the community :D

Show 9 more comments

1

  • 1

    Its basis is the answer of Chris: https://stackoverflow.com/a/11908079/4438007 ; may I suggest placing the link to it, instead of the question link?

  • 1

    @Jeffersonquesado for sure ! did not know he had this resource :P

  • Correct the answer. Instead of nome='maria' the correct is nome LIKE 'maria%'. According to him: Tipo: primeiro as MARIAS e abaixo a continuação por ordem alfabética e ID DESC.

  • Marias = plural of MARIA...

  • @Leonardobonetti button share the answer, you get the proper link

  • Ahhh, got it... sorry, my bad, like 'maria silva', my code would stick to just 'maria'

  • @Valdeirpsr ready, thank you, was totally wrong kkk

Show 2 more comments

-1

If you wanted to specifically display the list of a given name and then the rest of the data, as in the example you quoted.

You can do it through marriage( UNION ) of two consultations.

But for the sorting to work you will need to create a column to define the sorting priority, as in the example below:

SELECT Id, Nome, 0 Ordem
FROM Pessoas 
WHERE nome LIKE 'Maria%'
UNION ALL
SELECT Id, Nome, 1 Ordem 
FROM Pessoas 
WHERE nome NOT LIKE 'Maria%'
ORDER BY Ordem, Nome

Follow online example: Sqlfiddle

Responding to your comment. Failed to create column Order in query:

SELECT *, 0 Ordem FROM noticias WHERE bairro_id=5 
UNION ALL 
SELECT *, 1 Ordem FROM noticias WHERE bairro_id <> 5 
ORDER BY Ordem, id DESC
  • That’s not what he said

  • There is already an answer with UNION

  • @Leonardobonetti was asked to order "Marias", it was not?

  • @Robertodecampos Caique’s answer puts the column ordem which actually allows you to select the Marias first and then the other people. The other answer with UNION is wrong

  • I’m not disputing the correct answer, but your first answer is incorrect !

  • @Leonardobonetti , it was not he who edited and the answer answers the question, how to leave Marias at the top.

  • This is my real query using your example: SELECT * FROM noticias WHERE bairro_id=5 UNION ALL SELECT * FROM noticias WHERE bairro_id<>5 ORDER BY id DESC she must return me from the NOTICIAS table first the neighborhood news whose ID = 5 and then continue with the other neighborhoods. I did according to your example but the result is still the same. It shows the list, but in the end sorts only by ID DESC

  • @Cicero Missed the "plus" column note in my reply that I create a column in select and name it as Order, edited my answer including your query to help you.

Show 3 more comments

-1

Thanks a lot:

My problem has been solved with the query

SELECT * FROM noticias WHERE bairro_id = 5 OR bairro_id != 5 ORDER BY CASE WHEN (bairro_id = 5) THEN 0 ELSE 1 END, id DESC

The example with the table above was just to emphasize. The idea is that the system returns me a list ordered by neighborhoods, first putting a certain neighborhood at the top and continue the list with the other neighborhoods.

Ai is the post for other users with the same difficulty.

Thank you all.

  • 1

    Now that I’ve seen your answer. (1) I think the filter WHERE bairro_id = 5 OR bairro_id != 5 is completely useless; (2) In the text you quote primeiro as MARIAS e abaixo a continuação por ordem alfabética e ID DESC; are 3 criteria for ordering! The code you posted as a solution does not meet this requirement.

  • The Marys are missing ...

  • Besides the marias, that’s right @Marconciliosouza! rs

Browser other questions tagged

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