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
✅ 2. Connect to MySQL Database
✅ 3. Basic WHERE Clause
Fetch a user with a specific id:
Output example:
✅ 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,LIMITfor efficient queries. -
Use async/await for better readability in production code.
-
Use
INandLIKEfor flexible search queries.
