Skip to content
Home » [Update] เทคนิคการเขียน Subqueries ง่ายๆใน SQLite | inv ย่อมาจาก – NATAVIGUIDES

[Update] เทคนิคการเขียน Subqueries ง่ายๆใน SQLite | inv ย่อมาจาก – NATAVIGUIDES

inv ย่อมาจาก: คุณกำลังดูกระทู้

Meet Subquery

Subquery คือการเขียน query ซ้อน query โดยตัวคิวรี่ที่อยู่ข้างในจะเรียกว่า “inner/ sub query” และคิวรี่ที่อยู่ด้านนอกจะเรียกว่า “outer query” วิธีสังเกตง่ายๆว่าคิวรี่ไหนเป็น subquery ให้มองหา select ที่อยู่ในวงเล็บแบบนี้

SELECT ... FROM (SELECT ... FROM ...);

Subquery ช่วยให้เราเขียน query ที่มีความซับซ้อนขึ้น (ความคิดสร้างสรรค์กระฉูด 555+) โดยรวมหลายๆคิวรี่เข้าด้วยกันและรันทีเดียว ลำดับการรันจะเริ่มจาก inner most query ชั้นในสุดก่อนและค่อยๆไล่ออกมาชั้นนอก

👩‍💻 ถ้าใครยังไม่เคยเขียน SQL ลองอ่านบทความสอนพื้นฐานของเราได้ที่นี่ ดาวน์โหลดไฟล์ตัวอย่าง chinook.db

Simple Example

มาลองดูตัวอย่างแรกกันก่อน สมมติเราอยากจะรู้ชื่อเพลงที่มีขนาดใหญ่ที่สุด maximum bytes จากตาราง tracks เราต้องเขียนสอง queries โดยคิวรี่แรกใช้หา max(bytes) และนำผลลัพธ์ที่ได้ไปใส่ในคิวรี่ที่สองใน where clause

-- first query
SELECT MAX(bytes) FROM tracks; --1059546140

-- second query
SELECT * FROM tracks WHERE bytes = 1059546140;

เราสามารถรวมสองคิวรี่ด้านบนด้วยเทคนิค subquery ได้เลย (query ซ้อน query) ตามตัวอย่างด้านล่าง สังเกตว่า select ตัวที่สองจะอยู่ในเครื่องหมาย () หรือพูดง่ายๆวิธีการดูว่า query นั้นเป็น subquery หรือเปล่า ให้ดูว่ามีการเขียน select อยู่ในวงเล็บหรือเปล่า ง่ายกว่าที่คิด!

SELECT * FROM tracks
WHERE bytes = (SELECT MAX(bytes) FROM tracks);

🎬 ตัวอย่างการเขียน subquery ง่ายๆใน where clause เพื่อดึงข้อมูลออกมาจากตาราง tracks

Subquery in Where Clause

👩‍💻 ดึงชื่อและนามสกุลลูกค้าที่อยู่ในประเทศที่ชื่อขึ้นต้นด้วยตัว “U”

result set ที่เราต้องการ

มาลองดูอีกหนึ่งตัวอย่างใน where clause สมมติเราต้องการดึงชื่อลูกค้าที่อยู่ในประเทศที่ชื่อขึ้นต้นด้วยตัว “U” ผลลัพธ์ของ subquery จะออกมาสองประเทศคือ USA และ United Kingdom

SELECT firstname, lastname, country FROM customers 
WHERE country IN (
  SELECT DISTINCT country FROM customers
  WHERE country LIKE 'U%');

เราสามารถเขียนทีละ query ตามลำดับนี้เพื่อให้ได้ผลลัพธ์เดียวกัน นำ result ที่ได้จาก query แรกไปใส่ใน where clause ของ query ที่สอง

-- inner query returns 'USA' and 'United Kingdom'
SELECT DISTINCT country FROM customers
WHERE country LIKE 'U%';

-- outer query returns customers living in these countries
SELECT firstname, lastname, country FROM customers
WHERE country IN ('USA', 'United Kingdom');

🎬 ตัวอย่างการเขียน subquery ใน where clause แบบใช้ in operator (subquery ดึงออกมามากกว่าหนึ่งค่า)

ตัวอย่างนี้แอดเขียนให้ดูง่ายๆโดยดึงข้อมูลจาก customers แค่ตารางเดียว เวลาทำงานจริงเรานิยมเขียน subquery เพื่อดึงข้อมูลจากหลายๆตารางพร้อมกัน ซึ่งการเขียนจะซับซ้อนขึ้นนิดหน่อย ลองดูหัวข้อถัดไป

A (Little) More Complicated Example

👩‍💻 จงหาไอดีและชื่อลูกค้าที่มี sum invoices สูงสุด 10 อันดับแรกในปี 2012

result set ที่เราต้องการ

โจทย์นี้เราจะลองเขียน inner query ใน join clause โดยฟิลเตอร์ข้อมูลในตาราง invoices มาเฉพาะปี 2012 เท่านั้น เราใช้ฟังก์ชัน strftime เพื่อปรับ format คอลัมน์ invoicedate ให้แสดงเฉพาะปี %Y

เสร็จแล้วเราก็ join ข้อมูลในตาราง customers กับ invoices (ที่ฟิลเตอร์มาแล้ว) เข้าด้วยกัน โดยผลลัพธ์มีทั้งหมด 3 คอลัมน์คือ ไอดีลูกค้า ชื่อจริงลูกค้า และผลรวมของ invoices ทั้งหมดในปี 2012 เรียงข้อมูลจากมากไปน้อยแบบ descending order และแสดงผลเฉพาะลูกค้า 10 คนแรกที่มี sum invoices สูงที่สุดในปีนั้น

SELECT 
  cust.customerid, 
  cust.firstname, 
  sum(inv.total) sum_inv 
FROM customers cust
JOIN (SELECT * FROM invoices 
      WHERE STRFTIME("%Y", invoicedate) = '2012') inv
ON cust.customerid = inv.customerid
GROUP BY 1, 2
ORDER BY 3 DESC
LIMIT 10;

สรุปขั้นตอนการทำงานของ subqueries ในหัวข้อนี้

  1. inner query – select ข้อมูลจากตาราง invoices โดยฟิลเตอร์เฉพาะปี 2012 เท่านั้น
  2. outer query – select และ join ทั้งสองตารางเข้าด้วยกันชื่อย่อ as {cust, inv} ตามลำดับ
  3. หาผลรวมของคอลัมน์ total จับกลุ่มตามไอดีและชื่อจริงลูกค้า เรียงคอลัมน์นี้จากมากไปน้อย desc และแสดงผลเฉพาะลูกค้า 10 คนแรกที่มี total invoices สูงที่สุดในปี 2012

[su_spoiler title=”อีกหนึ่ง solution สำหรับโจทย์ข้อนี้”]

เราสามารถ join สองตารางและเขียน where เพื่อฟิลเตอร์เฉพาะข้อมูลในปี 2012 เท่านั้น โดยไม่ต้องเขียน subquery ก็ได้ ทั้งสองคิวรี่ที่เราเรียนในหัวข้อนี้ได้ผลลัพธ์เหมือนกัน 100%

SELECT 
  cust.customerid, 
  cust.firstname, 
  sum(inv.total) sum_inv 
FROM customers cust
JOIN invoices inv ON cust.customerid = inv.customerid
WHERE strftime('%Y', inv.invoicedate) = '2012'
GROUP BY 1, 2
ORDER BY 3 DESC
LIMIT 10;

[/su_spoiler]

Key Takeaway

  • Subquery คือการเขียนคิวรี่ซ้อนคิวรี่ (inner + outer queries)
  • วิธีดูว่าคิวรี่ไหนเป็น subquery บ้าง ให้มองหา select ที่เขียนอยู่ในวงเล็บ
  • เราสามารถเขียน subquery ได้หลายตำแหน่งตั้งแต่ where from join และ select clauses
  • SQL เป็นภาษาที่ค่อนข้างยืดหยุ่น เวลาเราเขียน join ดึงข้อมูลมากกว่าหนึ่งตาราง subquery ช่วยเพิ่มลูกเล่นและทางเลือกใหม่ๆในการดึงข้อมูล (อ่านเรื่อง performance ของ join vs. subquery ได้ที่โพสต์ stackoverflow)

Share this:

Like this:

Like

Loading…

[NEW] wr แปลว่าอะไร ดูความหมาย ตัวอย่างประโยค หมายความว่า พจนานุกรม Longdo Dictionary แปลภาษา คำศัพท์ | inv ย่อมาจาก – NATAVIGUIDES

wrA book written in a colloquial style.
wrA certain movie was novelized – rather it was a scenario written for a movie was expanded as a novel and localized to Japanese.
wrActors, artists, musicians, and writers may use many forms including spoken and written words, actions, colors and sounds.
wrActually I wrote her a card.
wrAdmitting what he says, I still think that he is in the wrong.
wrAdmitting what you say, I still think he is wrong.
wrAdmitting what you say, I still think that you were wrong.
wrAdmitting what you say, I still think you are in the wrong.
wrAdmitting what you say, I still think you are wrong.
wrAdverbial time clauses (here ‘when’) write about the future in the present tense.
wrA few minor mistakes apart, your writing is good.
wrAfter I had handed in my report to the teacher, I had to start writing another.


\”ไท่เปีย คืออะไร\” กินเจ(ผัก)เมืองตรัง INV#3 จีระศักดิ์ ทับเที่ยง


\”ไท่เปีย\” คืออะไร
การต่อสู้ ออกกำลัง กับกินเจ..เกี่ยวข้องกันอย่างไร
ประเพณีถือศีลกินเจ(ผัก) จังหวัดตรัง 2559 INV3
คุณจีระศักดิ์ ทับเที่ยง
ศาลเจ้ากิ่วอ่องเอี๋ย ตรัง
Trang Vegetarian Festival 2016
ดูให้ชัดกด..HD มุมจอ..
ติดตาม กด Subscibe ใน youtube ด้วยน่ะ
กินเจเมืองตรัง
Copyright © 2016 by KikiUsaha, All Right
http://www.facebook.com/KikiGalleryThai

นอกจากการดูบทความนี้แล้ว คุณยังสามารถดูข้อมูลที่เป็นประโยชน์อื่นๆ อีกมากมายที่เราให้ไว้ที่นี่: ดูความรู้เพิ่มเติมที่นี่

\

ลุ้นซิ่งกับพี่ติมวอลล์ แค่ซื้อไอศกรีมวอลล์รสใดก็ได้…


ลุ้นซิ่งกับพี่ติมวอลล์ แค่ซื้อไอศกรีมวอลล์รสใดก็ได้…

IC Layout Interview Questions Samples


IC LAYOUT INTERVIEW QUESTIONS Draw the symbol of NMOS \u0026 PMOS Transistor? Explain each terminal and where its connecting?
What is mean by Latchup? What are the solution to solve latchup issues? How you will take care while doing layout?
What is mean by Guard ring? What are types of Guard rings? How it will help to reduce latchup? What is Deep Nwell guarding?
what is meant by antenna? what is the solution to reduce antenna effect in the layout? From where accumulated charges are coming? Where is the discharge path? How do jumper and diode will help? No place to add diode and jumper what you will do?
Explain about shielding? Types of shielding? What are the signals you will do shielding and why? Where you will connect shielded lines and why? Without shielding what will happen?
What is mean by crosstalk?
Describe Electromigration effect? \u0026 ways to reduce Electromigration during layouts?
What is ESD? how you will fix ESD problems in the layout?
What is WPE, LOD \u0026 STI? Explain with diagram.
What is matching? Types of matching and explain one by one? What will happen not doing matching?
Describe DFM?
What is STD cells? How you will decide the height of STD cells? What is mean by Track? What is mean by a pitch?
Draw the diagram of INV, NAND and OR gates.
To Be Continued… Please like and subscribe my channel and press the bell icon to get new video updates.

IC Layout Interview Questions Samples

โวลต์ แอมป์ วัตต์ คืออะไร?


โวลต์ แอมป์ วัตต์ คืออะไร? : แม้จะได้ยิน3คำนี้กันมาตลอดชีวิต แต่หลายคนยังคงงงกับความหมายว่าคืออะไรกันแน่ แล้วแต่ละอย่างใช้วัดค่าไฟฟ้าได้อย่างไีรบ้าง ใครยังข้องใจต้องดูค่ะ เคลียร์ครบจบทุกประเด็นแน่นอนค่ะ
ดูรายละเอียดสินค้า PSI เพิ่มเติมได้ที่ www.psi.co.th
satanswer
satupdate
psi

โวลต์ แอมป์ วัตต์ คืออะไร?

CEO ที่ดีมีหน้าที่อะไรกันแน่ – 4 ระดับของ CEO ที่คุณห้ามพลาด


CEO มีหน้าที่อะไรกันแน่?
.
หลายคนบอกว่า
มีหน้าที่ออกสินค้าใหม่ๆ
พัฒนาเทคโนโลยี
.
หลายคนบอกว่า
มีหน้าที่บริหารด้านการขาย
.
แต่หลายคนกลับบอกว่า
คอยบริหารคนในองค์กรก็พอ
.
สรุปแล้ว
เจ้าของต้องทำอะไรกันแน่
มาดูคำตอบในวีดีโอ
CEO 4 ระดับ ที่บ่งบอกว่า
ตอนนี้คุณกำลังโฟกัสที่ตรงไหน
และอะไรคือสิ่งท้าทาย สำหรับคุณ

CEO ที่ดีมีหน้าที่อะไรกันแน่ - 4 ระดับของ CEO ที่คุณห้ามพลาด

นอกจากการดูบทความนี้แล้ว คุณยังสามารถดูข้อมูลที่เป็นประโยชน์อื่นๆ อีกมากมายที่เราให้ไว้ที่นี่: ดูวิธีอื่นๆLEARN TO MAKE A WEBSITE

ขอบคุณมากสำหรับการดูหัวข้อโพสต์ inv ย่อมาจาก

Leave a Reply

Your email address will not be published. Required fields are marked *