sql - MySQL count rows from multiple tables using join -


i have 3 mysql tables:

**locations** id      location_name 1       london 2       new york 3       washington  **bookings** id      voucher_code    location_id 1       disc100         1 2       null            null 3       disc200         1 4       disc500         2 5       disc700         1  **vouchers** id      voucher_code    net_value 1       disc100         155.00 2       disc200         135.00 2       disc500         155.00 2       disc700         155.00 

i'm trying display count of voucher net values each location. example can see 'london' location has 3 vouchers have been redeemed in bookings table, of 2 155.00 values vouchers , 1 135.00. need write mysql query pull these counts out of database.

so using above tables example, sql query should result in following:

location            netvalue155     netvaue135 london              2               1 new york            1               0 washington          0               0 

below have written far:

select      l.location_name,     '' date_from,      '' date_to,     v.netvalue155 locations l  left join (select voucher_code, count(distinct id) netvalue155 vouchers net_value='155.00' group id) v 

select a.location_name, sum(netvalue155), sum(netvalue135) (select l.id ,l.location_name, case when v.net_value = 155 1 else 0 end netvalue155, case when v.net_value = 135 1 else 0 end netvalue135 locations l left join bookings b on l.id = b.location_id left join vouchers v on  b.voucher_code = v.voucher_code) right join locations l on l.id = a.id group a.location_name 

[sql fiddle] http://sqlfiddle.com/#!9/daefd/19


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -