mysql - Getting column name ambigous -
i have query in getting column 'month' ambigious . trying following query. please check nd me.
select sc.name sc, b.name brand, count(t.id) tc, sum(if(is_category_present = 1, 1, 0)) base outlet_categories oc inner join transactions t on oc.outlet_id = t.outlet_id inner join outlets o on o.id = t.outlet_id inner join brands b on t.brand_id = b.id inner join sale_channels sc on sc.id = o.sale_channel_id t.month = month
qualify column names. not clear right names are, these need table aliases:
select sc.name sc, b.name brand, count(t.id) tc, -------^ looks aggregation query, there no `group by`. sum(if(is_category_present = 1, 1, 0)) base --------------^ outlet_categories oc inner join transactions t on oc.outlet_id = t.outlet_id inner join outlets o on o.id = t.outlet_id inner join brands b on t.brand_id = b.id inner join sale_channels sc on sc.id = o.sale_channel_id t.month = month; ----------------^
three notes mysql:
- your
if()
not necessary. - i guessing
month
variable. if so, name else. - presumably, want
group by
.
a better version of query:
select sc.name sc, b.name brand, count(t.id) tc, sum(??.is_category_present) base outlet_categories oc inner join transactions t on oc.outlet_id = t.outlet_id inner join outlets o on o.id = t.outlet_id inner join brands b on t.brand_id = b.id inner join sale_channels sc on sc.id = o.sale_channel_id t.month = v_month group sc.name, b.name;
the variable month
has been renamed v_month
, no confused column name. mysql interpret t.month = month
t.month = t.month
-- assuming month
in t
table. and, i'm guessing t.month = t.month
not intention.
Comments
Post a Comment