அட்டவணையில் இருந்து தேர்ந்தெடுத்தல்
MySQL இல் ஒரு அட்டவணையில் இருந்து தரவைத் தேர்ந்தெடுக்க, "SELECT" அறிக்கையைப் பயன்படுத்தவும்.
எடுத்துக்காட்டு
"customers" அட்டவணையில் இருந்து அனைத்து பதிவுகளையும் தேர்ந்தெடுத்து, முடிவு பொருளைக் காட்டவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT * FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
SELECT * அனைத்து நெடுவரிசைகளையும் திருப்பித் தரும்
மேலே உள்ள குறியீட்டை "demo_db_select.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_select.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
[
{ id: 1, name: 'John', address: 'Highway 71'},
{ id: 2, name: 'Peter', address: 'Lowstreet 4'},
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 4, name: 'Hannah', address: 'Mountain 21'},
{ id: 5, name: 'Michael', address: 'Valley 345'},
{ id: 6, name: 'Sandy', address: 'Ocean blvd 2'},
{ id: 7, name: 'Betty', address: 'Green Grass 1'},
{ id: 8, name: 'Richard', address: 'Sky st 331'},
{ id: 9, name: 'Susan', address: 'One way 98'},
{ id: 10, name: 'Vicky', address: 'Yellow Garden 2'},
{ id: 11, name: 'Ben', address: 'Park Lane 38'},
{ id: 12, name: 'William', address: 'Central st 954'},
{ id: 13, name: 'Chuck', address: 'Main Road 989'},
{ id: 14, name: 'Viola', address: 'Sideway 1633'}
]
முக்கிய குறிப்புகள்:
- SELECT *: அட்டவணையில் உள்ள அனைத்து நெடுவரிசைகளையும் திருப்பித் தரும்
- கால்பேக் செயல்பாடு: மூன்று அளவுருக்களைக் கொண்டுள்ளது: பிழை, முடிவு மற்றும் புலங்கள்
- முடிவு பொருள்: ஒவ்வொரு வரிசையையும் ஒரு பொருளாகக் கொண்ட வரிசை
- பிழை கையாளுதல்: எப்போதும் பிழைகளைச் சரிபார்க்கவும்
நெடுவரிசைகளைத் தேர்ந்தெடுத்தல்
அட்டவணையில் உள்ள சில நெடுவரிசைகளை மட்டும் தேர்ந்தெடுக்க, நெடுவரிசை பெயரைத் தொடர்ந்து "SELECT" அறிக்கையைப் பயன்படுத்தவும்.
எடுத்துக்காட்டு
"customers" அட்டவணையில் இருந்து name மற்றும் address ஐத் தேர்ந்தெடுத்து, திருப்பிய அளிக்கப்பட்ட பொருளைக் காட்டவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT name, address FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
});
மேலே உள்ள குறியீட்டை "demo_db_select2.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_select2.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
[
{ name: 'John', address: 'Highway 71'},
{ name: 'Peter', address: 'Lowstreet 4'},
{ name: 'Amy', address: 'Apple st 652'},
{ name: 'Hannah', address: 'Mountain 21'},
{ name: 'Michael', address: 'Valley 345'},
{ name: 'Sandy', address: 'Ocean blvd 2'},
{ name: 'Betty', address: 'Green Grass 1'},
{ name: 'Richard', address: 'Sky st 331'},
{ name: 'Susan', address: 'One way 98'},
{ name: 'Vicky', address: 'Yellow Garden 2'},
{ name: 'Ben', address: 'Park Lane 38'},
{ name: 'William', address: 'Central st 954'},
{ name: 'Chuck', address: 'Main Road 989'},
{ name: 'Viola', address: 'Sideway 1633'}
]
செயல்திறன் குறிப்புகள்:
- குறிப்பிட்ட நெடுவரிசைகள்: தேவையற்ற தரவைத் தவிர்க்கவும்
- நெட்வொர்க் போக்குவரத்து: குறைந்த தரவை அனுப்பவும்
- நினைவக பயன்பாடு: சிறிய முடிவு செட்கள்
- வாசிப்பு தெளிவு: குறிப்பிட்ட தரவு தேவைகளைக் குறிக்கவும்
முடிவு பொருள்
மேலே உள்ள எடுத்துக்காட்டின் முடிவிலிருந்து நீங்கள் பார்க்க முடியும் என, முடிவு பொருள் ஒவ்வொரு வரிசையையும் ஒரு பொருளாகக் கொண்ட ஒரு வரிசையாகும்.
எ.கா., மூன்றாவது பதிவின் முகவரியைத் திருப்பி அனுப்ப, மூன்றாவது வரிசை பொருளின் முகவரி பண்புக்கூறைக் குறிக்கவும்:
எடுத்துக்காட்டு
மூன்றாவது பதிவின் முகவரியைத் திருப்பி அனுப்பவும்:
console.log(result[2].address);
இது இந்த முடிவை உருவாக்கும்:
Apple st 652
வரிசை அணுகல்
முடிவு வரிசை பூஜ்ஜிய-அடிப்படையிலான குறியீட்டைப் பயன்படுத்தி அணுகப்படுகிறது
பண்புக்கூறு அணுகல்
புள்ளி குறியீட்டைப் பயன்படுத்தி நெடுவரிசை மதிப்புகளை அணுகலாம்
JSON பொருள்
ஒவ்வொரு வரிசையும் நெடுவரிசை பெயர்களை விசைகளாகக் கொண்ட JSON பொருள்
புலங்கள் பொருள்
கால்பேக் செயல்பாட்டின் மூன்றாவது அளவுரு முடிவில் உள்ள ஒவ்வொரு புலத்தைப் பற்றிய தகவல்களைக் கொண்ட ஒரு வரிசையாகும்.
எடுத்துக்காட்டு
"customers" அட்டவணையில் இருந்து அனைத்து பதிவுகளையும் தேர்ந்தெடுத்து, புலங்கள் பொருளைக் காட்டவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
con.query("SELECT name, address FROM customers", function (err, result, fields) {
if (err) throw err;
console.log(fields);
});
});
மேலே உள்ள குறியீட்டை "demo_db_select_fields.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_select_fields.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
[
{
catalog: 'def',
db: 'mydb',
table: 'customers',
orgTable: 'customers',
name: 'name',
orgName: 'name',
charsetNr: 33,
length: 765,
type: 253,
flags: 0,
decimals: 0,
default: undefined,
zeroFill: false,
protocol41: true
},
{
catalog: 'def',
db: 'mydb',
table: 'customers',
orgTable: 'customers',
name: 'address',
orgName: 'address',
charsetNr: 33,
length: 765,
type: 253,
flags: 0,
decimals: 0,
default: undefined,
zeroFill: false,
protocol41: true
}
]
மேலே உள்ள எடுத்துக்காட்டின் முடிவிலிருந்து நீங்கள் பார்க்க முடியும் என, புலங்கள் பொருள் ஒவ்வொரு புலத்தையும் ஒரு பொருளாகக் கொண்ட ஒரு வரிசையாகும்.
எ.கா., இரண்டாவது புலத்தின் பெயரைத் திருப்பி அனுப்ப, இரண்டாவது வரிசை உருப்படியின் பெயர் பண்புக்கூறைக் குறிக்கவும்:
எடுத்துக்காட்டு
இரண்டாவது புலத்தின் பெயரைத் திருப்பி அனுப்பவும்:
console.log(fields[1].name);
இது இந்த முடிவை உருவாக்கும்:
address
| புலம் பண்புக்கூறு | விளக்கம் |
|---|---|
| name | நெடுவரிசையின் பெயர் |
| table | அட்டவணையின் பெயர் |
| db | தரவுத்தளத்தின் பெயர் |
| type | தரவு வகை குறியீடு |
| length | புலத்தின் அதிகபட்ச நீளம் |
| flags | புலக் கொடிகள் |
மேம்பட்ட தேர்ந்தெடுக்கும் நுட்பங்கள்
வரிசைப்படுத்துதல் மற்றும் வரம்பிடுதல்
ORDER BY மற்றும் LIMIT உடன் தரவைத் தேர்ந்தெடுக்கவும்:
// Select with ordering and limiting
con.query("SELECT * FROM customers ORDER BY name ASC LIMIT 5", function (err, result) {
if (err) throw err;
console.log(result);
});
வெவ்வேறு மதிப்புகளைத் தேர்ந்தெடுத்தல்
தனித்துவமான மதிப்புகளைப் பெற DISTINCT ஐப் பயன்படுத்தவும்:
// Select distinct values
con.query("SELECT DISTINCT address FROM customers", function (err, result) {
if (err) throw err;
console.log(result);
});
கணக்கீடுகளுடன் தேர்ந்தெடுத்தல்
தேர்ந்தெடுக்கும் போது கணக்கீடுகளைச் செய்யவும்:
// Select with calculations
con.query("SELECT COUNT(*) as total_customers FROM customers", function (err, result) {
if (err) throw err;
console.log("Total customers:", result[0].total_customers);
});
மாறிகளுடன் தேர்ந்தெடுத்தல்
மாறிகளைப் பயன்படுத்தி மாறக்கூடிய தேர்வுகளைச் செய்யவும்:
// Select with variables
const columnName = "name";
const tableName = "customers";
const sql = `SELECT ${columnName} FROM ${tableName}`;
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
தேர்ந்தெடுக்கும் சிறந்த நடைமுறைகள்
பாதுகாப்பு
- SQL injection தடுக்க கேள்விக்குறி இடம் கொண்டவைகளைப் பயன்படுத்தவும்
- பயனர் உள்ளீட்டை எப்போதும் சரிபார்க்கவும்
- தரவு சுத்திகரிப்பு செயல்பாடுகளைப் பயன்படுத்தவும்
- பிழை செய்திகளை வெளிப்படையாகக் காட்ட வேண்டாம்
செயல்திறன்
- தேவையான நெடுவரிசைகளை மட்டும் தேர்ந்தெடுக்கவும்
- பெரிய முடிவு செட்களுக்கு LIMIT ஐப் பயன்படுத்தவும்
- செயல்திறனுக்காக பொருத்தமான குறியீடுகளைப் பயன்படுத்தவும்
- தேவையற்ற தரவைத் தேர்ந்தெடுப்பதைத் தவிர்க்கவும்
குறியீடு தரம்
- மறுபயன்பாட்டிற்காக SQL வினாக்களைக் காப்பாற்றவும்
- சரியான பிழை கையாளுதலையும் கண்காணிப்பையும் பயன்படுத்தவும்
- வாசிப்புக்காக வினாக்களை வடிவமைக்கவும்
- நிலையான குறியீட்டு மரபுகளைப் பின்பற்றவும்
முழுமையான எடுத்துக்காட்டு
அனைத்து தேர்ந்தெடுக்கும் நுட்பங்களையும் உள்ளடக்கிய முழுமையான எடுத்துக்காட்டு:
const mysql = require('mysql');
// Create connection
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "company_db"
});
// Connect to MySQL
con.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err.message);
return;
}
console.log('Connected to MySQL database');
// Example 1: Select all records
console.log('\n1. All customers:');
con.query("SELECT * FROM customers", (err, result) => {
if (err) {
console.error('Error selecting all customers:', err.message);
return;
}
console.log(result);
});
// Example 2: Select specific columns
console.log('\n2. Customer names and addresses:');
con.query("SELECT name, address FROM customers", (err, result) => {
if (err) {
console.error('Error selecting specific columns:', err.message);
return;
}
console.log(result);
});
// Example 3: Select with ordering and limiting
console.log('\n3. First 5 customers ordered by name:');
con.query("SELECT * FROM customers ORDER BY name ASC LIMIT 5", (err, result) => {
if (err) {
console.error('Error selecting with order and limit:', err.message);
return;
}
console.log(result);
});
// Example 4: Get field information
console.log('\n4. Field information:');
con.query("SELECT name, address FROM customers", (err, result, fields) => {
if (err) {
console.error('Error getting field information:', err.message);
return;
}
console.log('Fields:', fields);
// Close connection after all queries
setTimeout(() => {
con.end((err) => {
if (err) {
console.error('Error closing connection:', err.message);
return;
}
console.log('\nConnection closed');
});
}, 1000);
});
});