简体   繁体   中英

MySQL Select SUM of columns from one table based on the column of an other

I'd like to be able to Select the SUM total items that we have sold in the year based on a description.

DROP TABLE IF EXISTS items;

CREATE TABLE items
(quantity INT NOT NULL
,description  VARCHAR(30) NOT NULL
,price DECIMAL(5,2)
,jobID INT NOT NULL
,itemID SERIAL PRIMARY KEY
);

INSERT INTO items VALUES
(2,'Embroidered Shirt',10.00,5,1),
(5,'Embroidered Hoodie',15.00,5,2),
(3,'Printed Sticker',5.00,5,3);

DROP TABLE IF EXISTS jobs  ;

CREATE TABLE jobs  
(jobID SERIAL PRIMARY KEY
,invoiceStatus VARCHAR(12) NULL
);

INSERT INTO jobs VALUES
(5,'invoiced'),
(3,NULL),
(1,NULL);

DROP TABLE IF EXISTS invoices;

CREATE TABLE invoices  
(invoiceID SERIAL PRIMARY KEY
,Date DATE NOT NULL
,jobID INT NOT NULL
,INDEX(jobid,date)
);

INSERT INTO invoices VALUES
(2000,'2020/11/05',5);
+----------+--------------------+-------+-------+----------------------+
| items    |                    |       |       |                      |
+----------+--------------------+-------+-------+----------------------+
| quantity | description        | price | jobID | itemID (Primary Key) |
+----------+--------------------+-------+-------+----------------------+
| 2        | Embroidered Shirt  | 10.00 | 5     | 1                    |
| 5        | Embroidered Hoodie | 15.00 | 5     | 2                    |
| 3        | Printed Sticker    | 5.00  | 5     | 3                    |
+----------+--------------------+-------+-------+----------------------+

+---------------------+---------------+
| jobs                |               |
+---------------------+---------------+
| jobID (Primary key) | invoiceStatus |
+---------------------+---------------+
| 5                   | invoiced      |
| 3                   | NULL          |
| 1                   | NULL          |
+---------------------+---------------+

+-------------------------+-----------+-------+
| invoices                |           |       |
+-------------------------+-----------+-------+
| invoiceID (Primary key) | Date      | jobID |
+-------------------------+-----------+-------+
| 2000                    | 11/5/2020 | 5     |
+-------------------------+-----------+-------+

I would like to get the SUM total of all items that include the term 'embroider' in the description from jobs where the invoiceStatus is 'invoiced' in the current year.

So in the above example I would be looking for the result:

£95.00

I've tried all kinds of Joins, Selects within Selects but I just keep getting errors.

ie

SELECT jobs.jobID, jobs.invoiceStatus, invoices.invoiceDate, 
(SELECT SUM(items.price * items.quantity) AS embroideryTotal 
WHERE items.jobID=jobs.jobID 
AND jobs.jobID=invoices.jobID
AND jobs.invoiceStatus = 'invoiced' 
AND YEAR(FROM_UNIXTIME(invoices.invoiceDate)) = 2020
AND description LIKE '%embroider%' 

The code will be implemented into PHP at a later date but for now I just wish to be able to search the MySQL database via phpMyAdmin's SQL console.

I hope this now meets the MCRE

SELECT SUM(quantity * price) total
  FROM items i 
  JOIN jobs j 
    ON j.jobid = i.jobid 
  JOIN invoices x 
    ON x.jobid = j.jobid
 WHERE i.description LIKE '%embroider%'
   AND x.date BETWEEN CONCAT(YEAR(CURDATE()),'-01-01') AND CONCAT(YEAR(CURDATE()),'-12-31');
 +-------+
 | total |
 +-------+
 | 95.00 |
 +-------+

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