我是编程新手,我必须为当天创建我的任务摘要。
我有3个表格countries
一个用于task
,一个用于customers
这是表格定义
CREATE TABLE `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(2) NOT NULL DEFAULT '',
`name` varchar(100) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
CREATE TABLE `customer` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`country` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `country` (`country`)
);
CREATE TABLE `task` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`date` date NOT NULL,
`design_type` enum('Digitize','Vector','Quote') NOT NULL DEFAULT 'Digitize',
`order_type` enum('New','Edit','Revision') NOT NULL DEFAULT 'New',
`customer_id` int(10) unsigned NOT NULL,
`priority` enum('High','Medium','Low') NOT NULL Default 'High',
`remarks` text NULL,
PRIMARY KEY (`id`),
KEY `customer_id` (`customer_id`)
);
现在我必须创建这样的摘要。
---------------------------------------------------------------
| Country Name | New | Revision | Edit | Total Order |
+----------------+-------+-----------+--------+---------------+
| Australia | 2 | 1 | 0 | 3 |
| Pakistan | 4 | 0 | 2 | 6 |
| United State | 3 | 1 | 1 | 5 |
| United Kd | 2 | 2 | 0 | 4 |
+----------------+-------+-----------+--------+---------------+
我已经构建了一些虚拟数据sqlfiddle.com
任何建议将不胜感激。 非常感谢
答案 0 :(得分:1)
经过一段时间在互联网上查找和搜索我真的没有任何关于这篇文章的想法。但是通过付出更多的努力,我猜它可以通过聚合函数来解决,最后我得到了渴望的结果。这是我最后的询问..
SELECT c.name, c.code,
SUM(IF(t.order_type = 'New',1,0)) new,
SUM(IF(t.order_type = 'Revision',1,0)) revision,
SUM(IF(t.order_type = 'Edit',1,0)) edit
FROM task AS t
JOIN customer AS cus ON cus.id = t.customer_id
JOIN countries AS c ON c.id = cus.country
WHERE t.date = CURRENT_DATE AND t.status != 'Completed'
GROUP BY c.id