 |
ถามเกี่ยวกับ sql ครับ (การแสดง count จาก 2 ตาราง ) |
|
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2009-12-27 13:30:45 |
By :
somparn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พจน์ช่วยตอบเลย   
|
 |
 |
 |
 |
Date :
2009-12-27 13:54:45 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แหล่ะครับ ต้องใช้ subquery
Code
select t1.id_q ,t1.pt ,t2.pt
from (select count(id_q) as pt ,id_q where table1 group by id_q) as t1
left join (select count(id_q) as pt ,id_q where table2 goupt by id_q) as t2
on t1.id_q=t2.id_q
order by t1.id_q
ที่จริงผมต้องการรวมค่าสองตารางที่นับมาก็เพิ่ม + ไป *.*
Code
select t1.id_q ,t1.pt+t2.pt
from (select count(id_q) as pt ,id_q where table1 group by id_q) as t1
left join (select count(id_q) as pt ,id_q where table2 goupt by id_q) as t2
on t1.id_q=t2.id_q
order by t1.id_q
กรณีใช้ sum ก็แทนที่ count ไปใช้ได้เหมือนกันครับ
Code
select t1.id_q ,t1.pt+t2.pt
from (select sum(id_q) as pt ,id_q where table1 group by id_q) as t1
left join (select sum(id_q) as pt ,id_q where table2 goupt by id_q) as t2
on t1.id_q=t2.id_q
order by t1.id_q
ถึอว่าโพสไว้เผื่อคนมาดูทีหลังจะได้ทำได้เหมือนกัน   
|
 |
 |
 |
 |
Date :
2009-12-27 14:10:17 |
By :
xbeginner01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถามเองตอบเองซะงั้น แต่ก็มีประโยชน์ thank 
|
 |
 |
 |
 |
Date :
2009-12-27 14:38:26 |
By :
naskw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กำลังหาพอดีเลยค่ะ ขอบคุณค่ะ
|
 |
 |
 |
 |
Date :
2010-07-18 22:42:29 |
By :
mzchewiize |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้จริงอะ เหมือน syntax จะผิดอยู่ หรือว่าพิมพ์ผิด 
|
 |
 |
 |
 |
Date :
2010-07-18 23:46:26 |
By :
PlaKriM |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|