简体   繁体   中英

SQL Create view to count total number of orders

Please help create view for the task:

"Create a view containing information on the total number of orders for each book. The view must contain two columns: "book title" and "number of orders"."

The book title is "name", table book - kniga and orders - zakaz

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

But I guess I should get

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

Here is my example: https://www.db-fiddle.com/f/gtXitKPGLYwz5QQPr1LZDX/2

Or here:

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');

You can try this query:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM