我需要为客户货运单据创建一个视图,其中包含下一个信息: 好名字, 好计数 好的价格。
价格可能与一项购买协议不同。对于每个购买协议,可能存在几批装运。 client_shipment_details中的每个商品都可以有序列号,但是client_shipment文档 只保留这种商品的总数。 这是我的桌子:
purchase_agreement;
+------------------------------+--------------+------+-----+---------+-------- --------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| purchase_agreement_status_id | bigint(20) | YES | MUL | NULL | |
+------------------------------+--------------+------+-----+---------+----------------+
purchase_agreement_details;
+-----------------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| count | int(11) | NO | | NULL | |
| price | float | NO | | NULL | |
| purchase_agreement_id | bigint(20) | YES | MUL | NULL | |
| good_id | bigint(20) | YES | MUL | NULL | |
+-----------------------+------------+------+-----+---------+----------------+
goods;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
client_shipment;
-----------------------+--------------+------+-----+---------+---------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| jhi_date | datetime | YES | | NULL | |
| purchase_agreement_id | bigint(20) | YES | MUL | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
client_shipment_details;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
|
| serial_number | varchar(255) | YES | | NULL | |
| client_shipment_id | bigint(20) | YES | MUL | NULL | |
| good_id | bigint(20) | YES | MUL | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
这是我的查询:
SELECT
temp1.cs_id, temp1.good_id, temp1.count, temp1.price,good.name
FROM
(
SELECT csd.cs_id AS cs_id, csd.good_id AS good_id, csd.count AS count, pad.price AS price
FROM
(
SELECT client_shipment_id AS cs_id, good_id, count(*) AS count
FROM client_shipment_details
GROUP BY client_shipment_id, good_id
) AS csd
LEFT JOIN
(
SELECT pad.good_id AS good_id, pad.price AS price, cs.id AS cs_id
FROM purchase_agreement_details pad
JOIN client_shipment cs ON pad.purchase_agreement_id = cs.purchase_agreement_id
) AS pad ON csd.good_id = pad.good_id
GROUP BY cs_id,good_id
) AS temp1
JOIN goods ON temp1.good_id = good.id
ORDER BY cs_id,good_id;
一些测试数据:
insert into purchase_agreement values
(1,1),
(2,1);
insert into purchase_agreement_details values
(1,2,100,1,1),
(2,4,200,1,2),
(3,6,300,1,3),
(4,7,400,1,4),
(5,3,110,2,1),
(6,8,210,2,2),
(7,4,310,2,3),
(8,5,410,2,4);
insert into goods values
(1,"desk"),
(2,"sofa"),
(3,"chair"),
(4,"bed");
insert into client_shipment values
(1,'2019-02-21 09:57:00',1),
(2,'2019-02-21 09:57:00',1),
(3,'2019-02-21 09:57:00',2),
(4,'2019-02-21 09:57:00',2);
insert into client_shipment_details values
(1,"1-0001",1,1),
(2,"1-0002",1,1),
(3,"2-0001",1,2),
(4,"2-0002",1,2),
(5,"3-0001",1,3),
(6,"3-0002",1,3),
(7,"4-0001",1,4),
(8,"4-0002",1,4),
(9,"1-0003",2,1),
(10,"1-0004",2,1),
(11,"2-0003",2,2),
(12,"2-0004",2,2),
(13,"3-0003",2,3),
(14,"3-0004",2,3),
(15,"4-0003",2,4),
(16,"4-0004",2,4),
(17,"1-0005",3,1),
(18,"1-0006",3,1),
(19,"2-0005",3,2),
(20,"2-0006",3,2),
(21,"3-0005",3,3),
(22,"3-0006",3,3),
(23,"4-0005",3,4),
(24,"4-0006",3,4),
(25,"1-0007",4,1),
(26,"1-0008",4,1),
(27,"2-0007",4,2),
(28,"2-0008",4,2),
(29,"3-0007",4,3),
(30,"3-0008",4,3),
(31,"4-0007",4,4),
(32,"4-0008",4,4);
我想得到这样的结果:
client_shipment_id good_name price count
1 desk 100 2
1 sofa 200 2
1 chair 300 2
1 bed 400 2
2 desk 100 2
2 sofa 200 2
2 chair 300 2
2 bed 400 2
3 desk 110 2
3 sofa 210 2
3 chair 310 2
3 bed 410 2
4 desk 110 2
4 sofa 210 2
4 chair 310 2
4 bed 410 2