从单个查询创建多个视图表

时间:2017-11-26 18:24:32

标签: mysql sql

我有一个创建视图表的查询,如下所示:

CREATE OR REPLACE VIEW view_table AS
SELECT ………………
       ………………
FROM shop_table 
     JOIN …………  ON ………… = ………… 
     JOIN …………  ON ………… = ………… 
WHERE shopid = 1 
GROUP BY e.employee_name 
ORDER BY COUNT(p.payment_amount) ASC LIMIT 1

我的数据库中有20家商店。从WHERE shopid = 1可以看到,上面的查询将创建包含单个商店数据的视图表。

现在,我想在运行此查询时为我的数据库中的每个(全部20个)商店创建一个视图表,而不仅仅是一个。

我该怎么做?我是否必须每次创建20个不同的查询并更改WHERE子句中的值,或者是否有更简单的方法来实现此目的?

2 个答案:

答案 0 :(得分:1)

在MySQL中有多种方法可以做到这一点,它们都不是很好。我相信MySQL 8将ROW_NUMBER()RANK()使其更多更清洁,更快,但是现在这就是我在视图中做这样的事情... < / p>

https://www.db-fiddle.com/f/xxcCnsJWgbXBxhRx39bUV9/0

create table shop (
  id int
);

create table employee (
  id int,
  shop_id int
);

create table sale (
  employee_id int,
  payment_amount int
);


insert into shop values (1);
insert into shop values (2);
insert into shop values (3);


insert into employee values (1, 1);
insert into employee values (2, 1);
insert into employee values (3, 1);

insert into employee values (4, 2);
insert into employee values (5, 2);
insert into employee values (6, 2);

insert into employee values (7, 3);
insert into employee values (8, 3);
insert into employee values (9, 3);


insert into sale values (1, 100);
insert into sale values (1, 200);

insert into sale values (2, 300);

insert into sale values (3, 50);
insert into sale values (3, 50);
insert into sale values (3, 50);


insert into sale values (4, 200);
insert into sale values (4, 300);

insert into sale values (5, 400);

insert into sale values (6, 150);
insert into sale values (6, 150);
insert into sale values (6, 150);

insert into sale values (7, 200);
insert into sale values (7, 100);

insert into sale values (8, 250);

insert into sale values (9, 100);
insert into sale values (9, 150);
insert into sale values (9, 150);


create view shop_employee_total_sale as
  select
    employee_id, shop_id, sum(payment_amount) total_payment
  from
    sale
  inner join
    employee
      on employee.id = sale.employee_id
  group by
    employee_id, shop_id
;

create view shop_top_employee as
  select
    shop_employee_total_sale.shop_id,
    shop_employee_total_sale.employee_id,
    shop_employee_total_sale.total_payment
  from
    shop_employee_total_sale
  inner join
  (
    select shop_id, max(total_payment) as total_payment from shop_employee_total_sale group by shop_id
  )
    shop_top_total
      on  shop_top_total.shop_id       = shop_employee_total_sale.shop_id
      and shop_top_total.total_payment = shop_employee_total_sale.total_payment
;

select * from shop_top_employee;

select * from shop_top_employee where shop_id = 1;

select * from shop_top_employee where shop_id = 2;

select * from shop_top_employee where shop_id = 3;

请注意,我故意选择了一个例子,其中一个商店有两名员工,总付款相同。在这种情况下,他们都是“最佳表现者”,并为该商店返回两条记录。

编辑: 举例来说,当您访问RANK()时,它的改进方式......

CREATE VIEW better_shop_top_employee AS
  SELECT
    employee_id,
    shop_id,
    SUM(payment_amount) total_payment,
    RANK() OVER (PARTITION BY shop_id ORDER BY SUM(payment_amount) DESC)  shop_rank
  FROM
    sale
  INNER JOIN
    employee
      ON employee.id = sale.employee_id
  GROUP BY
    employee_id,
    shop_id
;

SELECT * FROM better_shop_top_employee WHERE shop_id = 1 AND rank = 1

答案 1 :(得分:0)

您需要一个存储过程。这是一个关于在mysql中使用存储过程的教程的链接

http://www.mysqltutorial.org/stored-procedures-parameters.aspx

如果你想在另一个查询中将结果作为表本身使用,那么函数就是可行的方法,但它可以实现的目标更加有限。 (你的例子非常简单)