Skip to content
Advertisement

How i take a value that is like field in my database?

I try to get values that contains words in my sql database. I try with “contains” but I get an error. I work with SQL

This is my code:

router.post('/busca',isLoggedIn, async (req,res) => {
            const {busca} = req.body;
            const buscar = Object.values({busca});
            const bus = await pool.query('SELECT * FROM links WHERE title CONTAINS ?',[buscar]);
            res.render('links/busca',{buscar, bus});

        });

I have this error:

UnhandledPromiseRejectionWarning: Error: ER_PARSE_ERROR: 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 ‘(title, ‘a’)’ at line 1

Advertisement

Answer

You can use LIKE operator :

SELECT * FROM links WHERE title LIKE '%word%';

if you are more than one word use and or or operator

SELECT * FROM links WHERE title LIKE '%word%' AND title LIKE '%secondword%';
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement