I need an SQL query to only return one data,

Asked

Viewed 59 times

-3

For example, I have a table, with 2 columns, Name and Letter. The table Name, the respective names are inserted, and the table letter, only accepts 3 letters, A, B and C.

The same name can have all three letters. I wanted to select only when a name has only the letter A. If it has the letter A and also B, I don’t want it to appear in select. More or less like this. If anyone can help me thank!!

  • Or even WHERE 'A' IN (letra_1, letra_2)

  • Sorry I explained wrong. I tried to rephrase the question but could not. Follow exactly what I want: For example, I have a table, with 2 columns, Name and Letter. The table Name, the respective names are inserted, and the table letter, only accepts 3 letters, A, B and C. The same name can have 3 letters. I wanted to select only when a name has only the letter A. If it has the letter A and also B, I don’t want it to appear in select. More or less like this. If anyone can help me thank!!

1 answer

0


Well, I guess this query should do the trick:

SELECT TABELA.*
  FROM TABELA
  LEFT JOIN TABELA T2
    ON T2.NOME = TABELA.NOME
   AND T2.LETRA IN ('B', 'C')
 WHERE TABELA.LETRA = 'A'
   AND T2.NOME IS NULL

I don’t have Mysql installed to test, so there might be some difference in dialect.

  • 1

    You can try db-fiddle.com if you want to.

  • Interesting that db-fiddle.com, I tested there and it worked right, thanks Anderson!

Browser other questions tagged

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