 |
SQL : count แบบ มา join กัน 2 ตารางได้หรือเปล่าคะ count จากตารางที่ 1 แต่ เอา id ที่ โชว์อยู่ไปอ้างอิงอีกตารางเพื่อให้ได้ชื่อ ตาม id นั้นนะคะ |
|
 |
|
|
 |
 |
|
เอาตรงนี้ไป test ดูครับ
select user.user_name
,count(export.user_id) as c_userid
from export,user
where export.user_id=user.user_id
group by user.user_name
ถ้าไม่ได้ก็ลองเปลียน group by user.user_name ดูครับ
|
 |
 |
 |
 |
Date :
2010-11-18 16:04:06 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่า ขอบคุณมากๆ เลยอะ ...
อยากคิดได้แบบนี้ อ่า ง่ายแล้วนะเนี่ยะ ยังคิดไม่ได้เลย -..-
|
 |
 |
 |
 |
Date :
2010-11-18 16:09:19 |
By :
unzeen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าอยากได้ ทั้งชื่อนามสกุลเลยอะคะ
select user.user_name
,count(export.user_id) as c_userid
from export,user
where export.user_id=user.user_id
group by user.user_name
พอจับจุดได้แร้วว่า group by ด้วยอะไร ก็จะเลือกได้แค่ตัวนั้นใช่ป่าวคะ
อย่างนี้ ก็ต้องใช้วิธีต่อ field ก่อน
|
 |
 |
 |
 |
Date :
2010-11-18 16:14:37 |
By :
unzeen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
select CONCAT(user.firstname,user.lastname) as name,count(export.in_charge_of) as count_responsibility from export,user where export.in_charge_of=user.user_id and format(ETD_DATE,'yyyy-mm-dd')='$period_start' group by name
ไม่ได้อะ
|
 |
 |
 |
 |
Date :
2010-11-18 16:16:47 |
By :
unzeen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้า select field เพิ่ม ก็ต้องเพิ่มที่ group ด้วยครับ แบบนี้
select f.name,f.lastname from tb group by f.name,f.lastname
พดได้ field ที่ต้องการแล้วถึงจะเอามาจัดรูปแบบจะง่ายกว่าครับ
หรือไมก้เปลียน group by ของคุณเป็น group by user.firstname,user.lastname
|
 |
 |
 |
 |
Date :
2010-11-18 16:50:53 |
By :
kanchen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
select user.firstname as firstname,user.lastname as lastname,count(export.in_charge_of) as count_responsibility from export,user where export.in_charge_of=user.user_id and format(ETD_DATE,'yyyy-mm-dd')='$period_start' group by user.firstname,user.lastname
มัน warnning อะ ผิดตรงไหนหรอ
|
 |
 |
 |
 |
Date :
2010-11-19 08:54:16 |
By :
unzeen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ออ ไม่ผิดคะ ได้แล้ว ^ ^
|
 |
 |
 |
 |
Date :
2010-11-19 08:54:47 |
By :
unzeen |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งง in_charge_of คืออะไร??
|
 |
 |
 |
 |
Date :
2014-02-11 16:51:33 |
By :
น้ำผึ้ง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|