sql - Proper syntax for create view with a subquery? -
i want create view using code:
select t1.firstname, t1.lastname, t2.sum customer t1,(select customerid,sum(total) sum invoice group by(customerid)) t2 t1.customerid=t2.customerid
the way makes sense me do
create view t2 select customerid,sum(total) sum invoice group by(customerid) create view customerinvoices select t1.firstname, t1.lastname, t2.sum customer t1, t2 t1.customerid=t2.customerid
but gives me syntax error, i'm not sure how call first view in main view
as you've noticed, syntax not allowed in mysql (although allowed in other databases):
select t1.firstname, t1.lastname, t2.sum customer t1 join (select customerid,sum(total) sum invoice group by(customerid) ) t2 on t1.customerid = t2.customerid;
you can rephrase as:
select c.firstname, c.lastname, sum(total) sum customer c join invoice on c.customerid = i.customerid group c.firstname, c.lastname;
this work in view.