从另一个表

时间:2015-10-05 20:44:35

标签: sqlite

我正在使用SQLite并拥有3个表,项目,发票和订单。我有一个基于项目表的select语句,它选择了存储在该表中的字段,但是我还想要特定项目的发票的总和值以及特定项目的订单总和值。我知道如何单独获取所有值,但是我还没想出如何将订单/发票的Sum值输入项目数据行。它是我需要搜索的嵌套选择还是其他概念?

根据项目表选择

SELECT  project.projectID, project.project_title, project.end_date, project.project_manager, project_status.project_status
FROM project
LEFT JOIN project_status
ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 12

根据发票表选择,其中x将是第一个选择

中的项目ID
SELECT sum(invoice_net)
FROM invoice
WHERE projectID= x

根据订单表进行选择,其中x将是第一个选择

中的项目ID
SELECT sum(total_order)
FROM order
WHERE projectID = x

1 个答案:

答案 0 :(得分:1)

您没有提供示例数据,甚至没有提供足够的数据库架构来获得答案,以提供您需要的确切SQL。

您没有描述发票或订单表。我必须假设它们包含ProjectID属性。

您可以使用子选择:

http://www.techrepublic.com/article/use-sql-subselects-to-consolidate-queries/1045787/ https://msdn.microsoft.com/en-us/library/ff487138.aspx

您选择可能看起来像(未经测试):

SELECT
    project.projectID,
    project.project_title,
    project.end_date,
    project.project_manager,
    project_status.project_status,
    (SELECT sum(invoice.invoice_net) FROM invoice WHERE invoice.projectID = project.projectID),
    (SELECT sum(order.total_order) FROM order WHERE order.projectID = project.projectID)
FROM project
LEFT JOIN project_status
    ON project.project_statusID=project_status.project_statusID
WHERE project.project_statusID BETWEEN 1 AND 12

或加入Group By。参见:

SQL JOIN, GROUP BY on three tables to get totals

您必须按所有非聚合分组(通常这将是您从项目表中选择的所有字段),然后将聚合函数(总和)应用于您的详细信息属性(invoice_net和total_order)。

像这样(未经测试):

SELECT
    project.projectID,
    project.project_title,
    project.end_date,
    project.project_manager,
    project_status.project_status,
    sum(invoice.invoice_net),
    sum(order.total_order)
FROM project
LEFT JOIN project_status
    ON project.project_statusID=project_status.project_statusID
LEFT JOIN invoice
    ON project.projectID = invoice.projectID
LEFT JOIN order
    ON project.projectID = order.projectID
WHERE project.project_statusID BETWEEN 1 AND 12
GROUP BY project.projectID, project.project_title, project.end_date,
    project.project_manager, project_status.project_status

选择哪个?性能具有可比性,但查询优化器中的弱点可能导致一个人比另一个人更受青睐。如果这很重要,请测试性能。

对于一次性,如果连接表的视图尚不存在,我可能会使用子选择。但在实践中,视图确实存在或应该存在,在这种情况下,使用GROUP BY(或者如果需要,使用GROUP BY创建视图)非常自然。

所以真正的答案变成了(未经测试):

CREATE VIEW project_order_invoice AS
SELECT
    project.projectID,
    project.project_title,
    project.end_date,
    project.project_manager,
    project_status.project_status,
    invoice.invoice_net,
    order.total_order
FROM project
LEFT JOIN project_status
    ON project.project_statusID=project_status.project_statusID
LEFT JOIN invoice
    ON project.projectID = invoice.projectID
LEFT JOIN order
    ON project.projectID = order.projectID


SELECT
    projectID,
    project_title,
    end_date,
    project_manager,
    project_status,
    sum(invoice_net),
    sum(total_order)
FROM project_order_invoice
WHERE project_statusID BETWEEN 1 AND 12
GROUP BY projectID, project_title, end_date,
    project_manager, project_status

对于视图,您可能希望包含所有表中的所有属性,只有1个ID属性的副本。