Filtering with WHERE
Filtering rows with WHERE
So far every query returned all the rows. Add a WHERE clause to keep only the rows that match a condition:
Only rows where the condition is true are returned.
การกรองแถวด้วย WHERE
จนถึงตอนนี้ การค้นหาทุกคำคืนมา ทั้งหมดแถว เพิ่ม WHERE เพื่อเก็บเฉพาะแถวที่ตรงตาม เงื่อนไข:
SELECT name, score FROM student WHERE score >= 80;
จะแสดงเฉพาะแถวที่มีเงื่อนไขเป็นจริงเท่านั้น
Comparison operators
You can compare with:
=equal to<>not equal to<less than,>greater than<=at most,>=at least
Numbers are written plainly (80). Text must go in single quotes ('11B'):
ตัวดำเนินการเปรียบเทียบ
คุณสามารถใช้เปรียบเทียบได้ดังนี้:
=เท่ากับ<>ไม่เท่ากับ<น้อยกว่า,>มากกว่า<=มากสุด (หรือเท่ากับ),>=น้อยสุด (หรือเท่ากับ)
เลขเขียนธรรมดา (80) ข้อความต้องใส่ใน เครื่องหมายอัญลักษณ์เดี่ยว ('11B'):
SELECT name FROM student WHERE form = '11B';
Common mistakes
- Text values go in single quotes:
WHERE name = 'Ann'. WHEREcomes afterFROM.
ข้อผิดพลาดทั่วไป
- ค่าข้อความใส่ในอัญลักษณ์:
WHERE name = 'Ann' WHEREมาต่อจากFROM
WHERE filters rows
WHERE keeps only the rows whose condition is true. · WHERE จะเก็บเฉพาะแถวที่มีเงื่อนไขเป็นจริงเท่านั้น
Show the name and score of every student who scored 80 or more. · แสดง name และ score ของนักเรียนทุกคนที่คะแนนอยู่ 80 ขึ้นไป
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Show the name of every student in form 11B. (Text goes in single quotes.) · แสดง name ของนักเรียนทุกคนในชั้น 11B (ข้อความอยู่ในเครื่องหมายอัญQuotes)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่