我需要帮助才能提出疑问,希望你能帮助我。我有这些表:
|id | office |
|---|------------|
| 1 | office A |
| 2 | office B |
| 3 | office C |
| id | animal |
|-----|----------|
| 1 | dog |
| 2 | cat |
| 3 | bird |
| 4 | snake |
| id | animal | idOffice | date |
|--------|--------------|----------------|----------------------|
| 1 | dog | 1 | 2015-06-10 16:57:23 |
| 2 | cat | 1 | 2015-06-10 17:47:45 |
| 3 | dog | 2 | 2015-06-10 18:20:56 |
| 4 | snake | 3 | 2015-06-11 10:33:47 |
| 5 | bird | 2 | 2015-06-11 11:41:29 |
| 6 | snake | 2 | 2015-06-11 12:59:36 |
| 7 | cat | 3 | 2015-06-11 13:01:41 |
| 8 | dog | 2 | 2015-06-12 13:56:58 |
| 9 | cat | 3 | 2015-06-12 14:17:34 |
我需要在日期销售(2015-06-10 00:00:00 / 2015-06-12 23:59:59)之间显示此结果,显示每个办公室的所有动物的所有销售情况:
| Office | animal | num_sales|
|---------------|--------------|----------|
| Office A | dog | 1 |
| Office A | cat | 1 |
| Office A | bird | 0 |
| Office A | snake | 0 |
| Office B | dog | 2 |
| Office B | cat | 0 |
| Office B | bird | 1 |
| Office B | snake | 1 |
| Office C | dog | 0 |
| Office C | cat | 2 |
| Office C | bird | 0 |
| Office C | snake | 1 |
我尝试这个查询,但我不知道如何继续:
SELECT A.animal, IFNULL(S.count,0) as num_sales
FROM Animals AS A
LEFT JOIN (
SELECT animal, COUNT(*) AS count
FROM Sales
where idOffice = 1
GROUP BY animal
) AS S
ON A.animal = S.animal
ORDER BY A.id ASC;
并获得此结果,但仅适用于Office A,而不是日期之间:
| animal |num_sales|
|----------|---------|
| dog | 1 |
| cat | 1 |
| bird | 0 |
| snake | 0 |
我需要在一个查询中获取每个办公室和销售日期之间的信息。
我希望你能帮助我。感谢和最诚挚的问候。
答案 0 :(得分:0)
试试这个:)
SELECT O.office, A.animal, count(*) num_sales
FROM (Sales S INNER JOIN Animals A on S. animal=A.animal) INNER JOIN Offices O on S.idOffice=O.id
WHERE (S.date BETWEEN '2015-06-10 00:00:00' AND '2015-06-12 23:59:59')
GROUP BY O.id, A.id
编辑: 如果您想要0值,请改用此值:
SELECT O.office, A.animal, COALESCE(count(S.id),0) num_sales
FROM (Offices O JOIN Animals A) LEFT JOIN Sales S ON S.idOffice=O.id AND S.animal=A.animal
WHERE (S.date BETWEEN '2015-06-10 00:00:00' AND '2015-06-12 23:59:59') OR ISNULL(S.id)
GROUP BY O.id, A.id
答案 1 :(得分:0)
请考虑以下事项:
数据集:
DROP TABLE IF EXISTS offices;
CREATE TABLE offices
(office_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,office VARCHAR(12) NOT NULL UNIQUE
);
INSERT INTO offices VALUES
(1 ,'office A'),
(2 ,'office B'),
(3 ,'office C');
DROP TABLE IF EXISTS animals;
CREATE TABLE animals
(animal_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,animal VARCHAR(12) NOT NULL UNIQUE
);
INSERT INTO animals VALUES
(101 ,'dog'),
(102 ,'cat'),
(103 ,'bird'),
(104 ,'snake');
DROP TABLE IF EXISTS sales;
CREATE TABLE sales
(sale_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,animal_id INT NOT NULL
,office_id INT NOT NULL
,dt DATETIME
);
INSERT INTO sales VALUES
(1,101,1,'2015-06-10 16:57:23'),
(2,102,1,'2015-06-10 17:47:45'),
(3,101,2,'2015-06-10 18:20:56'),
(4,104,3,'2015-06-11 10:33:47'),
(5,103,2,'2015-06-11 11:41:29'),
(6,104,2,'2015-06-11 12:59:36'),
(7,102,3,'2015-06-11 13:01:41'),
(8,101,2,'2015-06-12 13:56:58'),
(9,102,3,'2015-06-12 14:17:34');
查询:
SELECT o.office
, a.animal
, COUNT(s.sale_id) total
FROM offices o
CROSS
JOIN animals a
LEFT
JOIN sales s
ON s.office_id = o.office_id
AND s.animal_id = a.animal_id
AND s.dt BETWEEN '2015-06-10 00:00:00' AND '2015-06-12 23:59:59'
GROUP
BY o.office_id
, a.animal_id;
+----------+--------+-------+
| office | animal | total |
+----------+--------+-------+
| office A | dog | 1 |
| office A | cat | 1 |
| office A | bird | 0 |
| office A | snake | 0 |
| office B | dog | 2 |
| office B | cat | 0 |
| office B | bird | 1 |
| office B | snake | 1 |
| office C | dog | 0 |
| office C | cat | 2 |
| office C | bird | 0 |
| office C | snake | 1 |
+----------+--------+-------+