Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Mysql Where in NodeJs

Mysql Where in NodeJs

Using WHERE in MySQL with Node.js lets you filter records based on conditions β€” perfect for finding specific users, products, or any other data.

Let’s break it down with real examples πŸ‘‡


βœ… Step 1: Install MySQL Package

bash

npm install mysql SELECT * FROM users WHERE age > 25;


Using mysql (Callback style)

js

const mysql = require('mysql');const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase'});connection.connect();const sql = 'SELECT * FROM users WHERE age > ?';const values = [25];connection.query(sql, values, (err, results) => { if (err) throw err; console.table(results); connection.end();});


Using mysql2 (Async/Await)

js

const mysql = require('mysql2/promise');async function getUsers() { const connection = await mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'myDatabase' }); const [rows] = await connection.execute('SELECT * FROM users WHERE age > ?', [25]); console.table(rows); await connection.end();}getUsers();


πŸ” More WHERE Examples

ConditionQuery
EqualWHERE name = 'Alice'
Greater thanWHERE age > 25
Less than or equalWHERE age <= 30
Multiple conditions (AND)WHERE age > 20 AND name = 'Bob'
OR conditionWHERE name = 'Alice' OR name = 'Bob'
IN clauseWHERE name IN ('Alice', 'Charlie')
LIKE (pattern search)WHERE name LIKE 'A%'


πŸ›‘οΈ Always Use Placeholders

js

'SELECT * FROM users WHERE name = ? AND age > ?'

Using placeholders (?) helps prevent SQL injection πŸ”’

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql