简体   繁体   English

MySQL Select 基于另一个表的列的一个表中的列的总和

[英]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.我希望能够 Select 基于描述我们在这一年销售的总物品。

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.我想从今年 invoiceStatus 为“开票”的工作中获取描述中包含“刺绣”一词的所有项目的总和。

So in the above example I would be looking for the result:所以在上面的例子中,我会寻找结果:

£95.00 95.00 英镑

I've tried all kinds of Joins, Selects within Selects but I just keep getting errors.我已经尝试了各种连接,选择中的选择,但我只是不断收到错误。

ie 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.该代码将在以后实施到 PHP 但现在我只希望能够通过 phpMyAdmin 的 SQL 控制台搜索 MySQL 数据库。

I hope this now meets the MCRE我希望这现在符合 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 |
 +-------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM