Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> SQL

วิธีใช้คำสั่ง SQL SELECT Statement

Databases ไม่แค่เก็บข้อมูล พวกเขาทำให้ Data เข้าถึงได้ง่าย ด้วยความช่วยเหลือเล็กน้อยจาก SELECT Statement คุณสามารถ Retrieve ชุดค่าใดๆ จาก Database ที่ต้องการได้

ในคู่มือนี้ เราจะอภิปรายว่า SQL SELECT Statement คืออะไร และทำงานอย่างไร เราจะผ่าน Examples ของ SELECT Statement ใน Action ช่วยคุณเข้าใจใช้บน SQL Server ได้

SQL SELECT คืออะไร?

คำสั่ง SELECT เลือก Data จาก Database Table ชื่อบอกเลย

คุณใช้ SELECT Statement Retrieve Data จาก 1 หรือหลาย Tables คุณ Select ทุก Rows ใน Table หรือ Rows ที่ Meet Condition หรือ Set of Conditions

SELECT Clauses ใช้ Structure นี้:

SELECT column1, column2 FROM table;

Statement นี้ Select Columns "column1" และ "column2" จาก Table "table"

เข้าใจ SQL ได้ง่ายกว่าด้วยตัวอย่าง เรามี Table ชื่อ employees มี Values ดังนี้:

id name title hired_date salary
1 Thomas Carlton Sales Representative 09-08-2019 29400
2 Lisa Nelson Sales Representative 12-09-2019 29400
3 Victoria Carlisle Sales Director 04-02-2017 37800

มี 3 Records ใน Database คุณเรียนรู้การสร้าง Table นี้ใน SQL INSERT และ SQL UPDATE Tutorials ของเรา

เพื่อ View Records ภายใน Database เราต้องใช้ SELECT Statement

Select Specific Columns: เลือก Column เฉพาะเจาะจง

Executive Team ขอ List ทุก Employees และ Salaries พวกเขาไม่ต้อง Info อื่น: แค่ Employee Names และ Salaries

เราใช้ SELECT Statement Retrieve Info นี้:

SELECT ID, name, title, hired_date, salary FROM employees; 

Query นี้คืน List ทุก Employee Names และ Salaries จาก Employees Table Result Set:

Name Salary
Thomas Carlton 29400
Lisa Nelson 29400
Victoria Carlisle 37800

คุณ Select Columns ได้เท่าไหร่ก็ตามจาก Table

Select All Columns: เลือกทุก Column

เราสามารถ Select ทุก Columns ใน Table ได้แบบนี้:

SELECT ID, name, title, hired_date, salary FROM employees; 

Query นี้ Verbose หรือ Wordy หากมี Columns มากกว่านี้จะยาวขึ้น นั่นคือที่มาของ Operator *

Operator * Select ทุก Columns ใน Table เรียกว่า Wildcard Operator และหมายถึง Everything

SELECT * FROM employees;

Query นี้ให้ Full Report ของ Values ทั้งหมดใน Table "employees":

id name title hired_date salary
1 Thomas Carlton Sales Representative 09-08-2019 29400
2 Lisa Nelson Sales Representative 12-09-2019 29400
3 Victoria Carlisle Sales Director 04-02-2017 37800

Select Based on a Condition: เลือกตามเงื่อนไข

คุณ Select Values จาก Database ที่ Meet Condition หรือ Set of Conditions ได้ด้วย WHERE Clause

สมมติเราต้องการหา List Employees ที่เป็น Sales Representatives ทำได้ด้วย WHERE Statement:

SELECT * FROM employees WHERE title = 'Sales Representative'; 

Query นี้ Select ทุก Columns จาก Employees Table เฉพาะ Records ที่ Value ของ title เท่ากับ Sales Representative จะคืนค่า:

id name title hired_date salary
1 Thomas Carlton Sales Representative 09-08-2019 29400

เราเขียน Tutorial แยกสำหรับ WHERE Clause อ่าน Tutorial WHERE Clause ของเรา

สรุป

คำสั่ง SELECT Statement เลือก Records จาก Database Table คุณ Select Specific Columns จาก Table โดยระบุ Column Names ใน Query คุณ Select ทุก Columns ใน Database ด้วย Wildcard Operator (*)

พร้อมท้าทาย? เขียน SELECT Queries ที่:

  • Retrieve List of Employee Names
  • Retrieves List of Employees ที่มี Title Sales Director
  • Retrieve List of Employee Titles และ Salaries

ตอนนี้คุณพร้อมใช้ SELECT Statement อย่าง SQL Professional แล้ว!