如何编辑数据透视查询的结果?

时间:2015-08-27 18:38:27

标签: php mysql crud

我一直在研究不同的CRUD,它只是一个表,但是如何在数据透视查询的结果上获得CRUD功能呢?

我在这里做了一个小提琴:http://www.sqlfiddle.com/#!9/12eb0/3/0

是否有任何解决方案可以帮助我编辑这些结果...或添加/删除?

编辑:在此处添加了代码:

CREATE TABLE IF NOT EXISTS `drivers` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`status` CHAR(1) NOT NULL,
PRIMARY KEY (`id`))
;

INSERT INTO `drivers` (`id`, `name`, `status`) VALUES
(1, 'Tony Stark', 'A'),
(2, 'Steve Rogers', 'A'),
(3, 'Bruce Banner', 'A')
;

CREATE TABLE IF NOT EXISTS `products` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(255) NOT NULL,
`status` CHAR(1) NOT NULL,
PRIMARY KEY (`id`))
;

INSERT INTO `products` (`id`, `description`, `status`) VALUES
(1, 'Sandwich Bread', 'A'),
(2, 'Buns', 'A'),
(3, 'Rolls', 'A')
;

CREATE TABLE IF NOT EXISTS `production_list_header` (
`list_number` VARCHAR(255) NOT NULL,
`date_created` DATE NOT NULL,
PRIMARY KEY (`list_number`))
;

INSERT INTO `production_list_header` (`list_number`, `date_created`) VALUES
('20150826112314', '2015-08-26'),
('20150827085030', '2015-08-27')
;


CREATE TABLE IF NOT EXISTS `production_list_detail` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`list_number` VARCHAR(255) NOT NULL,
`product_id` INT UNSIGNED NOT NULL,
`driver_id` INT UNSIGNED NOT NULL,
`quantity` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_production_list_detail_production_list_header_idx` (`list_number` ASC),
INDEX `fk_production_list_detail_products1_idx` (`product_id` ASC),
INDEX `fk_production_list_detail_drivers1_idx` (`driver_id` ASC),
CONSTRAINT `fk_production_list_detail_production_list_header`
FOREIGN KEY (`list_number`)
REFERENCES `production_list_header` (`list_number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_production_list_detail_products1`
FOREIGN KEY (`product_id`)
REFERENCES `products` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_production_list_detail_drivers1`
FOREIGN KEY (`driver_id`)
REFERENCES `drivers` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
;

INSERT INTO `production_list_detail` (`id`, `list_number`, `product_id`, `driver_id`, `quantity`) VALUES
(1, '20150826112314', 1, 1, 500),
(2, '20150826112314', 1, 2, 600),
(3, '20150826112314', 1, 3, 550),
(4, '20150826112314', 2, 1, 450),
(5, '20150826112314', 2, 2, 575),
(6, '20150826112314', 2, 3, 350),
(7, '20150826112314', 3, 1, 670),
(8, '20150826112314', 3, 2, 920),
(9, '20150826112314', 3, 3, 250)
;

查询为:

SELECT p.description,
SUM(IF(dr.name = 'Tony Stark', d.quantity, 0)) as 'Tony Stark',
SUM(IF(dr.name = 'Steve Rogers', d.quantity, 0)) as 'Steve Rogers',
SUM(IF(dr.name = 'Bruce Banner', d.quantity, 0)) as 'Bruce Banner'
FROM production_list_detail d
join production_list_header h on h.list_number = d.list_number
join products p on p.id = d.product_id
join drivers dr on dr.id = d.driver_id
where d.list_number = '20150826112314'
group by p.description;

如何以任何方式编辑该查询的结果... php,crud,mysql客户端程序?

0 个答案:

没有答案