0Pricing
Flask Academy · บทเรียน

กรอง เรียงลำดับ และจำกัดผลลัพธ์

ใช้ filter_by, order_by และการแบ่งหน้า

กรอง เรียงลำดับ และจำกัดผลลัพธ์ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Narrow Results with filter_by

Use filter_by() with keyword arguments to keep only rows that match a column exactly. It reads almost like English. 🎯

admins = User.query.filter_by(role="admin").all()

More Power with filter

When you need comparisons, reach for filter() and full column expressions instead of plain equality.

adults = User.query.filter(User.age >= 18).all()

Combine Conditions

Pass several expressions to filter() and they join with AND, so every condition must hold for a row to match.

q = User.query.filter(User.age >= 18, User.active == True)

OR Conditions

For an either-or match, wrap conditions in or_() so a row qualifies when any one of them is true.

from sqlalchemy import or_
q = User.query.filter(or_(User.age < 18, User.vip))

Sort with order_by

Use order_by() to sort results by a column. By default rows come back in ascending order.

users = User.query.order_by(User.name).all()

Newest First with desc

Wrap a column in desc() to flip the sort and put the largest or latest values at the top.

posts = Post.query.order_by(Post.created.desc()).all()

Cap the Count with limit

Add limit() to fetch only the first N rows. It keeps responses small and queries fast.

top = Post.query.order_by(Post.score.desc()).limit(5).all()

Skip Rows with offset

Use offset() to skip a number of rows before returning. Combined with limit, it gives you simple paging.

page2 = Post.query.limit(10).offset(10).all()

Real Pagination with paginate

Flask-SQLAlchemy bundles paging into paginate(), which returns items plus handy page metadata in one call.

p = Post.query.paginate(page=2, per_page=10)

Chain It All Together

Filter, order, and limit chain fluently, and nothing runs until you finish with all or first.

q = Post.query.filter_by(public=True).order_by(Post.id.desc()).limit(3)

Order Matters for Limits

Always pair limit() with order_by. Without a sort, which rows you get back is unpredictable.

Quick Check

You want the three highest-scoring posts. Which clause makes the score the top result?

Recap: Shaping Results

You can now filter, sort with order_by and desc, and trim output with limit, offset, and paginate. Powerful reads unlocked! 🚀

คำถามที่พบบ่อย

บทเรียน “กรอง เรียงลำดับ และจำกัดผลลัพธ์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กรอง เรียงลำดับ และจำกัดผลลัพธ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กรอง เรียงลำดับ และจำกัดผลลัพธ์”

ใช้ filter_by, order_by และการแบ่งหน้า คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “กรอง เรียงลำดับ และจำกัดผลลัพธ์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ดึงแถวด้วย query และ get
  2. กรอง เรียงลำดับ และจำกัดผลลัพธ์
  3. กำหนดความสัมพันธ์และคีย์ต่างประเทศ
  4. การรวมข้อมูลและการโหลดแบบช้ากับแบบเร็ว
← กลับไปที่ Flask Academy