Node.js MySQL Where

🚀 Node.js MySQL – WHERE Clause

The WHERE clause is used to filter records based on specific conditions.

We’ll use mysql2 (recommended) for all examples.


✅ 1. Install mysql2

npm install mysql2

✅ 2. Connect to MySQL Database


 


✅ 3. Basic WHERE Clause

Fetch a user with a specific id:


Output example:

[{ "id": 1, "name": "John Doe", "email": "john@example.com" }]

✅ 4. Multiple Conditions (AND / OR)

AND Condition


OR Condition



✅ 5. Comparison Operators


 


✅ 6. LIKE Operator (Search by Pattern)


 


✅ 7. IN Operator (Multiple Values)



✅ 8. Using Async/Await with WHERE Clause


 


✅ 9. Tips & Best Practices

  • Always use prepared statements (?) to prevent SQL injection.

  • Combine WHERE with ORDER BY, LIMIT for efficient queries.

  • Use async/await for better readability in production code.

  • Use IN and LIKE for flexible search queries.

You may also like...