mysql - how to join tables and 2 select together -
i have following 2 queries, how can join them together?
query1:
select product_name, count(product_name) count_product_name ps_order_detail id_shop = 1 group product_name order count_product_name desc limit 5
query2:
select count(*) count, concat(decade, '-', decade + 9) year (select floor(year(`birthday`) / 10) * 10 decade ps_customer) c group decade;
the first query top 5 product name ordered most.
the second query customer birthday year , group them every ten years.
i want know age group order top 5 product.
result should be
product name, years, count producta 1990-2009 100 producta 2000-2019 20 productb 1980-1999 20 productb 1990-2009 25 productb 2000-2019 20
...
i have third table have connection.
create table ps_orders( id_customer, id_order ); create table ps_customer( id_customer, birthday ); create table ps_order_detail( id_order, product_name );
i not sure how put them together, can input product name 1 one years.
select count(*) count, concat(decade, '-', decade + 9) year (select floor(year(birthday) / 10) * 10 decade ps_customer cu, ps_orders o, ps_order_detail od cu.id_customer = o.id_customer , o.id_order = od.id_order , od.product_name = 'product name a' ) c group decade;
use second query, instead of specifying particular product, put grouping. join subquery returns top 5 products limit it.
select product_name, concat(decade, '-', decade + 9) year, count(*) count (select p.product_name, floor(year(birthday) / 10) * 10 decade (select product_name, count(*) count_product_name ps_order_detail id_shop = 1 group product_name order count_product_name desc limit 5) p join ps_order_detail od on od.product_name = p.product_name join ps_orders o on o.id_order = od.id_order join ps_customer c on c.id_customer = o.id_customer od.id_shop = 1) x group product_name, decade
Comments
Post a Comment