 |
|
สอบถามเกี่ยวกับการ Join ตาราง DataBase คับ คือ ผมอยาก Join ตาราง ทั้ง 3 ตาราง ไม่รู้ทำไง |
|
 |
|
|
 |
 |
|
ขอ Database ได้มั๊ยละครับพร้อมกับตัวอย่างข้อมูลนะ
|
 |
 |
 |
 |
Date :
2009-08-25 12:58:01 |
By :
naimai |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
SELECT A.name_drug,A.total_sale,B.total_quantity
FROM
(
SELECT name_drug, mode, sum(quantity) As total_quantity
FROM drug
INNER JOIN mode
ON drug.id_mode = mode.id_mode
GROUP BY name_drug, mode
HAVING(sum(quantity) <= 2)
) A
INNER JOIN
(
SELECT name_drug,sum(quantity_sale) as total_sale
FROM drug_sale
GROUP BY name_drug
) B
ON A.name_drug = B.name_drug
WHERE B.total_sale > A.total_quantity
|
 |
 |
 |
 |
Date :
2009-08-25 13:45:53 |
By :
jezusmitsui |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ DataBase มี(โดยประมาณ)
drug - id_drug , name_drug , quantity , id_mode , lot
1 aaa 2 1 1
2 aaa 1 1 2
3 bbb 1 2 1
4 ccc 3 1 1
drug_sale - id_sale , id_drug , name_drug , quantity_sale
1 1 aaa 2
2 3 bbb 4
3 1 aaa 2
mode - id_mode , mode
1 P
2 S
คือ อยากให้แสดงใน GridView ประมาณว่าถ้า จำนวนโดยรวมของquantity น้อยกว่าจำนวนโดยรวมของ quantity_sale แล้วให้แสดงลงใน GridView คับ อย่างจากตาราง DataBase จะได้ดังรูปคับ
name_drug quantity mode
aaa 3 P
bbb 1 S
ประมาณนี้ คับ โดย Code จะดู ว่า ค่า Sum(ผลรวม)ของ quantity <= Sum(ผลรวม)ของquantity_sale หรือ ไม่ ถ้ามีให้แสดงคับ
ช่วยแนะนำทีคับบ ขอบคุณ๕ับบบบ
|
 |
 |
 |
 |
Date :
2009-08-25 15:30:53 |
By :
hero_big |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
select a.name_drug,sum(quantity) as qty,mode
from drug a,drug_sale b,mode c
where a.id_drug = b.id_drug and a.id_mode = c.id_mode
group by a.name,mode
having sum(b.quantity_sale) > sum(a. quantity)
|
 |
 |
 |
 |
Date :
2009-08-25 19:14:04 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ ตามที่คุณsuperpheak บอก ผมลองใช้ดู ค่า Sum ที่ได้ จะเกิน ตามตาราง คับ
เช่น
ในตาราง (ประมาณ)
drug - id_drug , name_drug , quantity , id_mode , lot
_________1_______aaa_________2________1_____ 1
_________2_______aaa_________1________1______2
สมมุติ ค่า Sum(quantity)ของ aaa < Sum(quantity_sale)
โดยเมื่อรัน Code แล้ว จะเป็น (โดยประมาณ)
name_drug , sum(quantity)
aaa_____________6(ที่จริงน่าจะเป็น 3)
แล้วลองดึงค่า Sum(quantity_sale) ก็จะแสดงออกมาเกินค่าในตารางด้วยคับ
ไม่รู้มันเกี่ยวกับ lot ของสินค้ารึเปล่า คับ มันเหมือนวนลูป คับ ไม่รู้ทำไง คับ ช่วยอีกหน่อยคับ
น่าจะใกล้ได้ แล้ว คับ
ขอบคุณมากๆๆ คับ
อีดนิดๆๆๆ ช่วยที คับ
|
 |
 |
 |
 |
Date :
2009-08-25 22:14:30 |
By :
hero_big |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า DataBase ของ คุณ hero_big เป็นดังนี้ นะครับ
drug - id_drug , name_drug , quantity , id_mode , lot
1 aaa 2 1 1
1 aaa 1 1 2
3 bbb 1 2 1
4 ccc 3 1 1
drug_sale - id_sale , id_drug , name_drug , quantity_sale
1 1 aaa 2
2 3 bbb 4
3 1 aaa 2
mode - id_mode , mode
1 P
2 S
สมมุติถ้าเราเขียน query แบบนี้นะครับ
Code (SQL)
SELECT a.name_drug, quantity,quantity_sale, mode
FROM drug AS a, drug_sale AS b, mode AS c
WHERE a.id_drug=b.id_drug And a.id_mode=c.id_mode
ค่าที่ไดจะประมาณนี้ครับ
name_drug quantity quantity_sale mode
aaa 2 2 p
aaa 2 2 p
aaa 1 2 p
aaa 1 2 p
bbb 1 4 s
ซึ่งการ summary output ของตัวนี้จะเท่ากับ query ข้างบนครับ
ปัญหาเกิดจากการ join table ที่ 2 table มี record ที่เหมือนกันมากกว่า 1
ยกตัวอย่าง ปัญหาของคุณ hero_big
sum(qty) ใน table drug = 3 แต่เมื่อ นำ ไป join กับ table drug_sale ซึ่งมี 2 record จึงได้เป็น 6 ครับ
หากเพิ่ม aaa ใน drug_sale เป็น 3 คุณ hero_big ก็จะได้ output เป็น 9 ครับ
สรุปคือถ้า table เป็นอย่างนี้ join กันตรงๆ ไม่ได้ครับ อาจต้องเขียนสอง query หรือ sub query ครับ
ถ้าใครมีวิธี join กันตรงๆ ได้ ขอความชี้แนะด้วยครับ
|
 |
 |
 |
 |
Date :
2009-08-27 11:20:03 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
query ครับ
Code (SQL)
select a.name_drug,sum(quantity) as qty,mode from drug as a,mode as b
where a.id_mode = b.id_mode
and a.id_drug = (select distinct id_drug from drug_sale where id_drug = a.id_drug group by id_drug having sum(quantity_sale) >= sum(a.quantity) )
Group By a.name_drug,mode
|
 |
 |
 |
 |
Date :
2009-08-27 11:35:42 |
By :
superpheak |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณคับบบบบบ
|
 |
 |
 |
 |
Date :
2009-08-28 12:42:43 |
By :
hero_big |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
cmd.CommandText = "SELECT info_com(comID,IP_Address,Computer_Name,User_Login,location)" & _
",[เมนบอร์ด].[รหัส] as [รหัสเมนบอร์ด],[เมนบอร์ด].[รหัสอุปกรณ์_Serial] as [รหัสอุปกรณ์_Serialเมนบอร์ด],[เมนบอร์ด].Socket as [Socketเมนบอร์ด],[เมนบอร์ด].[ยี่ห้อ/รุ่น] as [ยี่ห้อ/รุ่นเมนบอร์ด] " & _
",cpu.[รหัส] as [รหัสซีพียู],cpu.[รหัสอุปกรณ์_Serial] as [รหัสอุปกรณ์_Serialซีพียู],cpu.[Socket] as [Socketซีพียู],cpu.[ยี่ห้อ/รุ่น] as [ยี่ห้อ/รุ่นซีพียู] " & _
",ram.[รหัส] as [รหัสแรม],ram.[รหัสอุปกรณ์_Serial] as [รหัสอุปกรณ์_Serialแรม],ram.[บัส],ram.[ยี่ห้อ/รุ่น] as [ยี่ห้อ/รุ่นแรม] " & _
",hdd.[รหัส] as [รหัสhdd],hdd.[รหัสอุปกรณ์_Serial] as [รหัสอุปกรณ์_Serialhdd],hdd.[ความจุ_GB],hdd.[ยี่ห้อ/รุ่น] as [ยี่ห้อ/รุ่นhdd] " & _
",vga.[รหัส] as [รหัสvga],vga.[รหัสอุปกรณ์_Serial] as [รหัสอุปกรณ์_Serialvga],vga.[ความจำ],vga.[ยี่ห้อ/รุ่น] as [ยี่ห้อ/รุ่นvga] " & _
",[power].[รหัส] as [รหัสpower],[power].[รหัสอุปกรณ์_Serial] as [รหัสอุปกรณ์_Serialpower],[power].[wat],[power].[ยี่ห้อ/รุ่น] as [ยี่ห้อ/รุ่นpower] " & _
" FROM info_com JOIN [เมนบอร์ด] on info_com.comID = [เมนบอร์ด].inforID" & _
",JOIN cpu on info_com.comID = cpu.inforID " & _
",JOIN ram on info_com.comID = ram.inforID " & _
",JOIN hdd on info_com.comID = hdd.inforID " & _
",JOIN vga on info_com.comID = vga.inforID " & _
",JOIN [power] on info_com.comID = [power].inforID" & _
" where info_com.comID = '" & id & "'" & _
" GROUP BY comID"
..............> มันขึ้นว่า "ข้อผิดพลาดทางไวยากรณ์ในส่วนคำสั่ง FROM" ใครพอจะรู้ช่วยบอกวิธีแก้ทีน่ะครับ<...........................
|
 |
 |
 |
 |
Date :
2013-05-20 10:05:42 |
By :
parn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมว่าผิดตรง JOIN น่ะ
Code (SQL)
" FROM info_com JOIN [เมนบอร์ด] on info_com.comID = [เมนบอร์ด].inforID" & _
",JOIN cpu on info_com.comID = cpu.inforID " & _
",JOIN ram on info_com.comID = ram.inforID " & _
",JOIN hdd on info_com.comID = hdd.inforID " & _
",JOIN vga on info_com.comID = vga.inforID " & _
",JOIN [power] on info_com.comID = [power].inforID" & _
น่าจะเป็นประมาณ
Code (SQL)
" FROM info_com JOIN [เมนบอร์ด] on info_com.comID = [เมนบอร์ด].inforID" & _
" INNER JOIN cpu on info_com.comID = cpu.inforID " & _
" INNER JOIN ram on info_com.comID = ram.inforID " & _
" INNER JOIN hdd on info_com.comID = hdd.inforID " & _
" INNER JOIN vga on info_com.comID = vga.inforID " & _
" INNER JOIN [power] on info_com.comID = [power].inforID" & _
|
 |
 |
 |
 |
Date :
2013-05-20 11:27:56 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2013-05-21 16:42:54 |
By :
parn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2013-05-21 16:54:51 |
By :
parn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ DBMS ตัวไหนครับ Access หรือเปล่าครับ ??
|
 |
 |
 |
 |
Date :
2013-05-21 17:32:42 |
By :
CPU4Core |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช่แล้วครับ DBMS ของผมคือ Access 2012 ครับ
(คือมันมีผลจากการไม่ใช้ sql ยังไง ครับ)
|
 |
 |
 |
 |
Date :
2013-05-22 09:04:41 |
By :
parn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|