SQL how to select in 2 different columns

Asked

Viewed 1,695 times

1

I’m trying to make a SELECT to select 2 different columns from the same table, example:

   id   |   name   |      tags
-----------------------------------
    1   |   acao   |      null
    2   | aventura |      null
    3   |   rpg    |  acao,aventura

I tried so many ways to do this SELECT but I couldn’t get it to work:

SELECT * FROM jogos WHERE
name LIKE "%acao%",
tags LIKE "%acao%"

I want that when researched "acao" return this way:

   id   |   name   |       tags
-----------------------------------
    1   |   acao   |       null
    3   |   rpg    |  acao,aventura

I have no idea how else to make it work.

  • Not missing the OR operator, right? SELECT * FROM games WHERE name LIKE "%acao%" OR tags LIKE "%acao%"

1 answer

4


To attach instructions in clause WHERE you must use the word AND:

SELECT *
  FROM jogos
 WHERE name LIKE "%acao%"
   AND tags LIKE "%acao%"

If you want to test the search in any of the columns, regardless of the value of the other, use the word OR:

SELECT *
  FROM jogos
 WHERE name LIKE "%acao%"
    OR tags LIKE "%acao%"
  • 1

    Using this way it does not return me anything from any of the fields, using AND the line needs to have "action" both in the name field and in the tag to return something. Using "OR" worked, I can’t believe I forgot this hahahahh

  • See with the OR

Browser other questions tagged

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