sql创建视图来计算订单总数

j8yoct9x  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(321)

请帮助为任务创建视图:
“创建一个视图,其中包含有关每本书的订单总数的信息。视图必须包含两列:“书名”和“订单数”
书名是“名字”,表书-克尼加和订单-扎卡兹

create view info_books as select kniga.name  from zakaz, kniga group by kniga.name;
select * from info_books;

但我想我应该

name | count 
ee | 2 
ew | 1
few | 1

下面是我的例子:https://www.db-fiddle.com/f/gtxitkpglywz5qqpr1lzdx/2
或者在这里:

CREATE TABLE zakaz (
  `id_zakaz` INTEGER,
  `id_chit` INTEGER, 
  `data` INTEGER, 
  `id_knigi` INTEGER
);
CREATE TABLE kniga (
  `id_knigi` INTEGER,
  `author` CHAR(30), 
  `name` CHAR(30)
);

INSERT INTO zakaz
  (`id_zakaz`, `id_chit`, `data`, `id_knigi`)
VALUES
  ('1', '1', '123', '33'),
  ('5', '31', '414', '53'),
  ('7', '3', '523', '33'),
  ('4', '4', '52342', '54');

INSERT INTO kniga
  (`author`, `name`, `id_knigi`)
VALUES
  ('abc', 'ee', '33'),
  ('cfe', 'ew', '53'),
  ('feaq', 'fewda', '54');
8wtpewkr

8wtpewkr1#

您可以尝试此查询:

CREATE VIEW info_books AS
SELECT K.name, COUNT(*) AS 'Count'
FROM zakaz AS Z LEFT JOIN kniga AS K ON Z.id_knigi=K.id_knigi GROUP BY K.name;

SELECT * FROM info_books;

相关问题