Condition in a select

Asked

Viewed 156 times

0

I need to create a SELECT which contain only records of a value x onwards. For example, return only movies with number of copies greater than 5

My Select

router.get('/filmes', (req, res) =>{
    execSQLQuery('SELECT IF(copias > 5) from filmes', res);
});

Table Films

CREATE TABLE filme(
id int not null Primary Key Auto_Increment,
titulo varchar(255) not null,
diretor varchar(255) not null,
copias int not null
);

Error returned in Insomnia

{
    "code": "ER_PARSE_ERROR",
    "errno": 1064,
    "sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') from filmes' at line 1",
    "sqlState": "42000",
    "index": 0,
    "sql": "SELECT IF(copias > 5) from filmes"
}
  • SELECT * FROM filmes WHERE copias > 5

3 answers

6


To use conditions in Mysql you need to understand the clauses it has, then in this case you can use the WHERE (which means "Where", if translated), ie you will indicate to fetch the records WHERE the number is greater than 5, as I will show below.

SELECT * FROM filmes WHERE copias > 5

If you need to select between a certain number and another, for example from numbers 5 to 50, you can use the clause BETWEEN (meaning "Enter", if translated), that is, you will indicate to search for the records that are AMID two values, always this way, as I will show below.

SELECT * FROM filmes WHERE copias BETWEEN 5 AND 50 

If you need to select only the number 5, you can use the clause IN, (meaning "In", if translated), that is, you will indicate that you need only the records that contain the value 5 WITHIN field copies, this as I will show below

SELECT * FROM filmes WHERE copias IN (5)
  • Hello, the in is just like the Where ?

  • Where indicates where to search, and IN indicates to run the table and capture only the records with value 5 in the case of the above example. Where indicates that in the table movies I want to search in the field copies the IN values or inside this field that have value 5

  • read the answer and understand, I think I have made it clear enough, otherwise study the Mysql clauses. Where it means WHERE AND IN MEANS IN, you understand ?

  • will be in this case, but the IN, Voce can select more than one number, in the clause, for example IN (5,10,15,20)

  • Now I get it, !

  • like I said, study

Show 1 more comment

3

2

Browser other questions tagged

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