简体   繁体   中英

MySQL join 3 tables count

I need help to make a query, hope you can help me. I have these tables:

Table Offices

| |---|------------| | 1 | office A | | 2 | office B | | 3 | office C |

Table Animals

| |-----|----------| | 1 | dog | | 2 | cat | | 3 | bird | | 4 | snake |

Table Sales

| |--------|--------------|----------------|----------------------| | 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 |

I need this result between date sales(2015-06-10 00:00:00 / 2015-06-12 23:59:59), showing all sales of all animals for each office:

| |---------------|--------------|----------| | 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 |

I try this query but i dont know how to go on:

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;

and get this results but only for Office A and not between dates:

| |----------|---------| | dog | 1 | | cat | 1 | | bird | 0 | | snake | 0 |

I need to get this info for each office and between sales dates in one query.

I hope you can help me.Thanks and best regards.

try this one :)

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

EDIT: If you want 0 values, use this one instead:

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

Consider the following:

The data set:

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

The query:

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 |
     +----------+--------+-------+

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