【问题标题】:Joining four tables on multiple conditions在多个条件下连接四个表
【发布时间】:2023-03-05 23:13:01
【问题描述】:

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

其中一条记录必须是某家公司所有收据和发票的总金额。 所以我试着写这个查询:

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;

这工作正常。它立即向我显示数据。这是结果:

但是当我尝试对 invoice_total 和 receipt_total 进行 SUM 时(因为我想要这家公司在特定时期的营业额),我什么也得不到。这是我写的:

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 中创建表的方式。卡片详细信息和客户表与想要的结果完全无关。我插入数据也没有问题。

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

【问题讨论】:

  • 请以表格文本的形式向我们展示您当前的结果您期望的结果。
  • “结果仍然没有”不会帮助我们帮助您。什么都没有???没有答案?错误的答案?空值?最重要的是。正确答案是什么。
  • 这里是如何提问的指南。 stackoverflow.com/help/how-to-ask
  • 对不起:/ 第一次在这里发帖,我发现我的解释不是很好。我的意思是结果是NULL。我只想得到一个单列单行,其中包含每张收据和发票的总和。
  • 请阅读meta.stackoverflow.com/questions/333952/…并编辑您的问题以提供更多信息

标签: mysql sql join sum


【解决方案1】:

这是我的表格数据

公司表(5条记录)companies

存储表(19 条记录)stores

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

Invoices 表(还有数千条记录)invoices

【讨论】:

    【解决方案2】:

    请试试这个:

    编辑:

    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
    

    如果它没有给你一个错误,请告诉我。

    【讨论】:

    • 我刚试过这个,但是由于查询的持续时间(超过 30 秒)它失去了与 MYSQL 服务器的连接我不知道这是否与大量记录有关
    • 我刚刚尝试了编辑。仍然无法正常工作(它在 30 秒后与服务器断开连接并出现错误)。我认为问题在于我想要总结的信息量。谢谢
    【解决方案3】:
    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 语句以使数据更小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 2011-08-15
      相关资源
      最近更新 更多