1.SQL Select
เป็นคำสั่งที่ใช้สำหรับการเรียกดูข้อมูลในตาราง (Table) คำสั่ง SQL SELECT สามารถเรียกได้ทั้งตาราง หรือว่า สามารถระบุฟิวด์ที่ต้องการเรียกดูข้อมูลได้
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Sample1 การเลือกข้อมูลที่ระบุฟิวด์
Output
--------------------------------------------------------------------------------------
Syntax
SELECT Column1, Column2, Column3,... FROM [Table-Name]
Sample1 การเลือกข้อมูลที่ระบุฟิวด์
SELECT CustomerID, Name, Email FROM customer
Output
CustomerID
|
Name
|
Email
|
---|---|---|
C001
| Win Weerachai | win.weerachai@thaicreate.com |
C002
| John Smith | john.smith@thaicreate.com |
C003
| Jame Born | jame.born@thaicreate.com |
C004
| Chalee Angel | chalee.angel@thaicreate.com |
2.SQL AVG
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยหาค่าเฉลี่ยผลรวมของฟิวด์Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT AVG(Column/Field) AS [New-Field] FROM [Table-Name]
Sample1 การเลือกข้อมูลค่าเฉลี่ยผลรวมของ Budget
SELECT AVG(Budget) AS AvgBudget FROM customer
Output
AvgBudget
|
---|
2500000
|
---------------------------------------------------------------------------------------
3.SQL DROP
เป็นคำสั่งที่ใช้สำหรับการลบคุณสมบัติต่าง ๆ ของตาราง เช่น ลบตาราง , ลบฐานข้อมูล , ลบ IndexDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
- ลบฐานข้อมูล
DROP TABLE 'TableName'
- ลบตาราง
DROP DATABASE 'DatabaseName'
- ลบ Index
DROP INDEX TableName.IndexName (for SQL Server)
DROP INDEX TableName on IndexName (for Access)
DROP TABLE 'TableName'
- ลบตาราง
DROP DATABASE 'DatabaseName'
- ลบ Index
DROP INDEX TableName.IndexName (for SQL Server)
DROP INDEX TableName on IndexName (for Access)
-----------------------------------------------------------------------------------------
4.SQL UPDATE
เป็นคำสั่งที่ใช้สำหรับลบข้อมูลในตาราง (Table) โดยสามารถทำการลบได้หลาย Record ภายในคำสั่งเดียว หรือว่า Record เดียว ทั้งนี้ขึ้นอยู่กับ Where ที่ผูใช้เขียนขึ้นด้วย
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Table : country
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
DELETE FROM [Table-Name] WHERE clause
Table : country
CountryCode
|
CountryName
|
---|---|
TH
| Thailand |
EN
| English |
US
| United states |
JP
| Japan |
Sample1 การลบข้อมูลลงใน Table
DELETE FROM country WHERE CountryCode = 'JP'
Output
CountryCode
|
CountryName
|
---|---|
TH
| Thailand |
EN
| English |
US
| United states |
-----------------------------------------------------------------------------------------
5.SQL FIRST
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยเลือกข้อมูล แถวแรกของข้อมูลที่พบ
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
Sample1 การเลือกข้อมูลแบบด้วย FIRST ในตาราง customer
Output
Database : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
SELECT FIRST(ColumnName) FROM TableName
Sample1 การเลือกข้อมูลแบบด้วย FIRST ในตาราง customer
SELECT FIRST(Name) As Name FROM customer
Output
Name
|
---|
Weerachai Nukitram
|
------------------------------------------------------------------------------------------
6.MYSQL REPLACE()
เป็นคำสั่งของ MySQL ใช้ในการแทนที่ค่าใน String ที่ต้องการ
Database : MySQL
Syntax
Sample
SQL REPLACE
Database : MySQL
Syntax
REPLACE(str,from_str,to_str)
Sample
SELECT REPLACE('www.thaicreate.com', 'w', 'Ww');
-> WwWwWw.thaicreate.com
-> WwWwWw.thaicreate.com
SQL REPLACE
----------------------------------------------------------------------------------------
7.MYSQL WEEK()
เป็นคำสั่งของ MySQL กับ DateTime โดย WEEK ใช้ในการหาสัปดาห์ของปี
Database : MySQL
Syntax
Sample
Database : MySQL
Syntax
WEEK(date[,mode])
The following table describes how the
mode
argument works.Mode | First day of week | Range | Week 1 is the first week … |
---|---|---|---|
0 | Sunday | 0-53 | with a Sunday in this year |
1 | Monday | 0-53 | with more than 3 days this year |
2 | Sunday | 1-53 | with a Sunday in this year |
3 | Monday | 1-53 | with more than 3 days this year |
4 | Sunday | 0-53 | with more than 3 days this year |
5 | Monday | 0-53 | with a Monday in this year |
6 | Sunday | 1-53 | with more than 3 days this year |
7 | Monday | 1-53 | with a Monday in this year |
Sample
SELECT WEEK('2008-02-20');
-> 7
SELECT WEEK('2008-02-20',0);
-> 7
SELECT WEEK('2008-02-20',1);
-> 8
SELECT WEEK('2008-12-31',1);
-> 35
-> 7
SELECT WEEK('2008-02-20',0);
-> 7
SELECT WEEK('2008-02-20',1);
-> 8
SELECT WEEK('2008-12-31',1);
-> 35
----------------------------------------------------------------------------------------
8.SQL TRIM
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการตัดช่องว่างของข้อความDatabase : MySQL,Microsoft Access,SQL Server,Oracle
Syntax
TRIM(' My String ') = 'MyString'
LTRIM(' My String ') = 'MyString '
RTRIM(' My String ') = ' MyString'
LTRIM(' My String ') = 'MyString '
RTRIM(' My String ') = ' MyString'
Sample1 ตัดช่องว่างทั้งด้านซ้ายและด้านขวา
SELECT Trim(Name) AS MyString FROM customer
Output
MyString
|
---|
Win Weerachai
|
John Smith
|
Jame Born
|
Chalee Angel
|
----------------------------------------------------------------------------------------
9.SQL USE
เป็นคำสั่งที่ใช้ในการเปลี่ยนหรือเรียกใช้ฐานข้อมูลปัจจุบันDatabase : MySQL , SQL Server
Syntax
USE DatabaseName
----------------------------------------------------------------------------------------
10.MYSQL REPEAT()
เป็นคำสั่งของ MySQL ใช้ในการแทนที่แสดงจำนวนข้อความ ตามจำนวน Loop ที่ต้องการDatabase : MySQL
Syntax
REPEAT(str,count)
Sample
SELECT REPEAT('MySQL', 3);
-> MySQLMySQLMySQL
SELECT REPEAT('ThaiCreate', 5);
-> ThaiCreateThaiCreateThaiCreateThaiCreateThaiCreate
-> MySQLMySQLMySQL
SELECT REPEAT('ThaiCreate', 5);
-> ThaiCreateThaiCreateThaiCreateThaiCreateThaiCreate
ไม่มีความคิดเห็น:
แสดงความคิดเห็น