Pick up a single record for equal columns

Asked

Viewed 239 times

0

I have an example table:

produto    |   supermercado
----------------------------
    1      |        1
    4      |        2
    6      |        1
    5      |        1
    8      |        2
    7      |        3

I want to get all existing supermarkets in this table, without picking up repeat numbers. The result would be:

 supermercado
----------------
      1
      2
      3

Disregarding the repeated supermarkets. I couldn’t even begin with a query, because I do not know how to take these equal results.

3 answers

2


To get the same results just use the command DISTINCT in the query.

So it doesn’t return repeated values, it would look great:

SELECT DISTINCT carrinhos.supermercado FROM carrinhos 
  • select distinct field from table Where condition

1

Use the word "distinct" in your sql query, it has exactly the function of eliminating repeated values in the query:

select distinct supermercado from tabela

0

to make the DISTINCT of a single column uses DISTINCT ON.

select distinct on (ed.id) ed.id, ed.nome from eventodisparo ed

Browser other questions tagged

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