USERNAME EMAIL NAME PRODUCT RRP
381542 [email protected] Boys 1367 32559.63
ถ้าสมมติว่า ไม่ต้องการค่า RRP จะคิดออกมาในรูปแบบ
select us.username as USERNAME, us.email as EMAIL, wm.name as NAME, count( wp.id ) as PRODUCT
from users us, warranty_members wm, warranty_product wp
where us.id = wm.user_id
and wm.id = wp.warranty_member_id
group by wm.user_id
order by product desc
select
us.username as USERNAME,
us.email as EMAIL,
wm.name as NAME,
count( wp.id ) as PRODUCT,
sum(coalesce(wi.rrp, 0)) sum_rrp
from users us,
left join warranty_members wm
on us.id = wm.user_id
left warranty_product wp
on wm.id_members = wp.member_id
left join warranty_serials ws
on wp.serial = ws.serial and wp.warranty_no = ws.warranty_no
left join warranty_items wi on ws.item_code = wi.item_code
group by wm.user_id
order by product desc
select
us.username,
us.email,
wm.name,
count(wr.id) as product,
sum(coalesce(wi.rrp, 0)) as sum_rrp
from users us
left join warranty_members wm on us.id_user=wm.user_id
left join warranty_product wp on wm.id_member=wp.member_id
left join warranty_serials ws on wp.serial=ws.serial and wp.warranty_no=ws.warranty_no
left join warranty_items wi on ws.item_code=wi.item_code
group by wm.user_id
order by product desc
select
us.username,
us.email,
wm.name,
count(wp.id_register) as product,
sum(coalesce(wi.rrp, 0)) as sum_rrp
from users us, warranty_members wm, warranty_product wp, warranty_serials ws, warranty_items wi
where us.id_user=wm.user_id
and wm.id_member=wp.member_id
and wp.serial=ws.serial
and wp.warranty_no=ws.warranty_no
and ws.item_code=wi.item_code
group by wm.user_id
order by product desc
select
us.username,
us.email,
wm.name,
count(distinct wr.id) as product,
sum(if(wr.serial_no=ws.serial_no, wi.rrp, 0)) as sum_rrp
from users us
left join warranty_members wm
on us.id=wm.user_id
left join warranty_product_registrations wr
on wm.id=wr.warranty_member_id
left join warranty_serials ws
on wr.serial_no=ws.serial_no
and wr.warranty_no=ws.warranty_no
left join warranty_items wi
on ws.item_code=wi.item_code
where us.email is not null
and us.email <> ''
and wm.name is not null
and wm.name <> ''
group by wm.user_id
order by product desc
รายละเอียดของแต่ละสมาชิก
select
wr.product_category_name,
wr.model,
wr.serial_no,
wr.warranty_no,
if(wr.serial_no=ws.serial_no, wi.rrp, 0) as rrp
from users us
left join warranty_members wm
on us.id=wm.user_id
left join warranty_product_registrations wr
on wm.id=wr.warranty_member_id
left join warranty_serials ws
on wr.serial_no=ws.serial_no
and wr.warranty_no=ws.warranty_no
left join warranty_items wi
on ws.item_code=wi.item_code
where wm.user_id='381607'
group by wr.id