在多个条件下联接四个表

时间:2020-04-30 21:06:20

标签: mysql sql join sum

我很难创建此查询。我有一个包含数千条记录的数据库,我想创建报告。 我的数据库是这样的:DATABASE

其中一条记录必须获取特定公司的所有收据和发票的总额。 因此,我尝试编写此查询:

SELECT *
FROM (
SELECT r.receipt_total, i.invoice_total, s.store_id, c.company_id
FROM receipts r
INNER JOIN invoices i on r.store_id = i.store_id 
INNER JOIN stores s on r.store_id = s.store_id
INNER JOIN companies c on c.company_id=s.company_id ) t1;

这很好。它立即向我显示数据。结果如下: result

但是,当我尝试对invoice_total和receive_total进行总计时(因为我希望该公司在特定时期内的营业额),我什么也没有。这是我写的:

SELECT *, (SUM(t1.invoice_total) + SUM(t1.receipt_total)) AS TOTAL
FROM (
  SELECT r.receipt_total, r.receipt_datetime, i.invoice_datetime, i.invoice_total,
         s.store_id, c.company_id, c.company_name
  FROM receipts r
  INNER JOIN invoices i on r.store_id = i.store_id 
  INNER JOIN stores s on r.store_id = s.store_id
  INNER JOIN companies c on c.company_id=s.company_id 
  WHERE c.company_name= "Cogidoo"
  AND DATE(invoice_datetime) between "01-01-2019" and "31-03-2019"
  AND DATE(receipt_datetime) between "01-01-2019" and "31-03-2019") t1;

我也尝试过,但是结果仍然没有:

SELECT (SUM(receipt_total) + SUM(invoice_total)) AS SUM
FROM (
SELECT r.receipt_total, r.receipt_datetime, i.invoice_total, i.invoice_datetime, s.store_id, 
       c.company_id, c.company_name
  FROM receipts r
  INNER JOIN invoices i on r.store_id = i.store_id 
  INNER JOIN stores s on r.store_id = s.store_id
  INNER JOIN companies c on c.company_id=s.company_id ) t1
WHERE t1.company_name = "Jamia"
AND DATE(t1.receipt_datetime) BETWEEN "01-01-2019" AND "31-03-2019"
AND DATE(t1.invoice_datetime) BETWEEN "01-01-2019" AND "31-03-2019";

所以我最后的愿望是获取特定时期内某家公司的所有发票和收据的总和:)

我希望结果只是一个具有最终值的单元格

PS:添加更多信息 这就是我用Java创建表的方式。 carddetails和customer表根本与所需结果无关。另外,插入数据也没有问题。

dsl.createTableIfNotExists("companies")
                    .column("company_id",SQLDataType.BIGINT.nullable(false))
                    .column("company_uuid", SQLDataType.INTEGER.nullable(false))
                    .column("company_name", SQLDataType.VARCHAR(30).nullable(false))
                    .column("company_address", SQLDataType.VARCHAR(30).nullable(false))
                    .constraint(
                            primaryKey("company_id")
                    )
                    .execute();

            dsl.createTableIfNotExists("stores")
                    .column("store_id", SQLDataType.BIGINT.nullable(false))
                    .column("store_name", SQLDataType.VARCHAR(30).nullable(false))
                    .column("store_address", SQLDataType.VARCHAR(30).nullable(false))
                    .column("company_id", SQLDataType.BIGINT.nullable(true))
                    .constraints(
                            primaryKey("store_id"),
                            foreignKey("company_id").references("companies", "company_id"),
                            unique("store_name")
                    )
                    .execute();




            dsl.createTableIfNotExists("customers")
                    .column("customer_id", SQLDataType.BIGINT.nullable(false))
                    .column("customer_uuid", SQLDataType.INTEGER.nullable(false))
                    .column("customer_name", SQLDataType.VARCHAR(30).nullable(false))
                    .column("customer_address", SQLDataType.VARCHAR(30).nullable(false))
                    .constraints(
                            primaryKey("customer_id"),
                            unique ("customer_name")
                    )
                    .execute();

            dsl.createTableIfNotExists("carddetails")
                    .column("card_id", SQLDataType.BIGINT.nullable(false))
                    .column("card_number", SQLDataType.VARCHAR(30).nullable(false))
                    .column("card_type", SQLDataType.VARCHAR(30).nullable(false))
                    .column("card_contactless", SQLDataType.BOOLEAN.nullable(false))
                    .constraints(
                            primaryKey("card_id"),
                            unique ("card_number")
                    )
                    .execute();

            dsl.createTableIfNotExists("receipts")
                    .column("receipt_id", SQLDataType.BIGINT.nullable(false))
                    .column("receipt_total", SQLDataType.DOUBLE.nullable(false))
                    .column("receipt_datetime", SQLDataType.TIMESTAMP(0).nullable(false))
                    .column("receipt_payment", SQLDataType.VARCHAR(4).nullable(false))
                    .column("store_id", SQLDataType.BIGINT)
                    .column("card_id", SQLDataType.BIGINT.nullable(true))
                    .constraints(
                            primaryKey("receipt_id"),
                            foreignKey("store_id").references("stores", "store_id"),
                            foreignKey("card_id").references("carddetails", "card_id")
                    )
                    .execute();




            dsl.createTableIfNotExists("invoices")
                    .column("invoice_id", SQLDataType.BIGINT.nullable(false))
                    .column("invoice_total", SQLDataType.DOUBLE.nullable(false))
                    .column("invoice_datetime", SQLDataType.TIMESTAMP(0).nullable(false))
                    .column("invoice_payment", SQLDataType.VARCHAR(4).nullable(false))
                    .column("customer_id", SQLDataType.BIGINT)
                    .column("store_id", SQLDataType.BIGINT)
                    .column("card_id", SQLDataType.BIGINT)
                    .constraints(
                            primaryKey("invoice_id"),
                            foreignKey("store_id").references("stores", "store_id"),
                            foreignKey("card_id").references("carddetails", "card_id"),
                            foreignKey("customer_id").references("customers", "customer_id")
                    )
                    .execute();

3 个答案:

答案 0 :(得分:0)

请尝试:

编辑:

SELECT t.st_id, t.c_id, t.val1 + t.val2 
FROM (
SELECT s.store_id as st_id, c.company_id as c_id, SUM(r.receipt_total) as val1, SUM(i.invoice_total) as val2
FROM receipts r
INNER JOIN invoices i on r.store_id = i.store_id 
INNER JOIN stores s on r.store_id = s.store_id
INNER JOIN companies c on c.company_id=s.company_id
GROUP BY s.store_id, c.company_id
) as t

告诉我它没有给您带来错误。

答案 1 :(得分:0)

这是我的表格数据

Comapnies表(5条记录)companies

存储表(19条记录)stores

收据表(数千条记录,可能超过100,000条)receipts

发票表(也有数千条记录)invoices

答案 2 :(得分:0)

SELECT *, (t.val1+t.val2) as total
FROM (
SELECT s.store_name as store, SUM(r.receipt_total) as val1, SUM(i.invoice_total) as val2
FROM receipts r
INNER JOIN invoices i on r.store_id = i.store_id 
INNER JOIN stores s on r.store_id = s.store_id
INNER JOIN companies c on c.company_id=s.company_id
WHERE c.company_name = "Cogidoo"
AND DATE(i.invoice_datetime) between "2019-01-01" and "2019-01-31"
AND DATE(r.receipt_datetime) between "2019-01-01" and "2019-01-31"
group by s.store_name with rollup
) as t

这件事奏效。处理大约需要12秒。我添加了一些where语句以使数据更小。