Condition select

0

I need to create a SELECT that contains only records of an x value onwards. For example, returning only movies with number of copies greater than 5

My Select

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

FIlmes Table

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"
}
    
asked by anonymous 07.12.2017 / 13:41

3 answers

6

To use conditions in MySQL you need to understand the clauses it has, so in this case you can use WHERE (meaning "Where" if translated), ie you will be prompted to search the Where records 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 BETWEEN clause (meaning "Enter" if translated), ie you will indicate to search for records that are BETWEEN two values, always in 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 IN clause, (which means "Inside" if translated), meaning you need only the records that contain the value 5 INSIDE of the copies field, as I will show below

SELECT * FROM filmes WHERE copias IN (5)
    
07.12.2017 / 13:48
3
SELECT * FROM FILME WHERE COPIAS > 5

link

    
07.12.2017 / 13:49
2

Your SQL Query is wrong, the correct one would be:

SELECT * FROM filmes WHERE copias > 5

follows a syntax reference: link

    
07.12.2017 / 13:43