Php 将包含相同Id但不同数据的两行合并为一行

Php 将包含相同Id但不同数据的两行合并为一行,php,mysql,sql,database,relational-database,Php,Mysql,Sql,Database,Relational Database,我有一个非常棘手的问题要问大家 假设我有下列表格 表AccountType: 表客户账户: 表Customer_Account包含客户ID及其帐户类型ID的列表。Account_Type_Id是来自AccountType.Id的外键 假设在Customer_Account表中,一个名为Josh id 450的客户可以拥有一个支票帐户和一个储蓄帐户,如上所示。通过在AccountType表上进行两次左连接,我可以输出此客户及其id和帐户类型,如下所示: 输出将是: Id Accou

我有一个非常棘手的问题要问大家

假设我有下列表格

表AccountType:

表客户账户:

表Customer_Account包含客户ID及其帐户类型ID的列表。Account_Type_Id是来自AccountType.Id的外键

假设在Customer_Account表中,一个名为Josh id 450的客户可以拥有一个支票帐户和一个储蓄帐户,如上所示。通过在AccountType表上进行两次左连接,我可以输出此客户及其id和帐户类型,如下所示:

输出将是:

 Id        Account_Type_1         Account_Type_2           
450           Checking               Checking 
450           Savings                Savings
451           Checking               Checking   
我想做的是,如果像Josh id 450这样的客户同时拥有支票和储蓄账户,我想将上面的两行数据输出到一行,如下所示:

 Id        Account_Type_1      Account_Type_2
450           Checking            Savings
此外,如果客户只有一种类型的账户,如此处id为451的客户,我只希望该类型的账户出现在相应的列下,如下所示:

 Id        Account_Type_1      Account_Type_2
451           Checking            
或者,如果id为451的客户只有一个储蓄账户,则输出应为:

Id        Account_Type_1      Account_Type_2
451                              Savings
我希望“支票”仅显示在Accoun_Type_1下,而“储蓄”仅显示在Account_Type_2下。如果我按CustAccount.Customer\u Id分组,我会得到以下信息:

 Id        Account_Type_1      Account_Type_2
450           Checking            Checking
451           Checking            Checking
任何专家的帮助都将不胜感激

谢谢。

我想我应该帮你

:否则


应帮助您

在ON子句中添加更多条件:

ON Account1.Id = CustAccount.Account_Type_Id and Account1.Account_Type_Id = 1

ON Account2.Id = CustAccount.Account_Type_Id and Account2.Account_Type_Id = 2
将产生包含客户持有的账户的输出。如果他们只有一个帐户,那么其他帐户类型将为空

编辑:对不起,我没有意识到你没有一个客户表。您可以创建一个临时表,其中包含不同的Customer_Id值列表,并将其用作联接中的第一个表

select distinct Customer_Id into #Customers
    from Customer_Account
或者更直截了当:

select distinct C.Customer_Id,
    ( select 'Checking' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 1 ) as Account_Type_1,
    ( select 'Savings' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 2 ) as Account_Type_2,
    from Customer_Account as C

这看起来像是完全外部联接的直接应用程序:

SELECT COALESCE(ac1.id, ac2.id) AS id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
  FULL OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac1.Id = ac2.Id;
如果您的DBMS不支持完全外部联接,但支持左外部联接,则可以使用:

SELECT ac0.id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT DISTINCT c.Customer_ID AS Id FROM Customer_Account AS c) AS ac0
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
    ON ac0.id = ac1.id
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac0.Id = ac2.Id;

第一个子查询生成存在的客户ID列表;第二个生成账户类型1检查列表;第三个生成帐户类型2保存的列表。联接确保正确识别每个帐户。

我不明白您的表是什么样子的。表“Customers”有一个字段“Account\u Id”和一个字段“Customer\u Id”?“Josh”在这个表中出现两次,都是使用相同的Customer\u Id?表Customers有Id列、Name列、Account\u Type\u 1列、Account\u Type\u 2列和Account\u Type\u Id表accounttype中的外键我敢肯定您在这里会感到困惑。Account_Type_1是您在查询结果集中引入的名称,它似乎不是Customers表中的一列。抱歉,它实际上是Account_Id,而不是Account_Type_Id。Account_Type_1呢?至于第一个链接,问题与我的不同。在他们的问题中,他们的表中只有一列具有不同的值。我,我有两个列,交替使用不同的值,这是不同的。我真正想要的是一种分组方式,将这两行合并成表中的一行。至于工会,我相信它只在两种选择之间起作用。我真的不能只使用一个SELECT语句来实现这一点吗?AccountType表中没有列Account\u Type\u Id,但我明白你的意思。它不起作用,因为它仍然显示两次,并且现在它为第二个帐户类型输出NULL选择“Checking from Customer_account”?您可以使用查找从id 1获取帐户类型检查,或者只选择一个常量。相关子查询的结果将是常量值或NULL。COALESCE或CASE可用于提供默认值,例如n/a。尝试玩:选择42作为答案,选择“我”,其中1=2作为选择
select distinct C.Customer_Id,
    ( select 'Checking' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 1 ) as Account_Type_1,
    ( select 'Savings' from Customer_Account where Customer_Id = C.CustomerId and Account_type_Id = 2 ) as Account_Type_2,
    from Customer_Account as C
SELECT COALESCE(ac1.id, ac2.id) AS id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
  FULL OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac1.Id = ac2.Id;
SELECT ac0.id, ac1.Account_Type_1, ac2.Account_Type_2
  FROM (SELECT DISTINCT c.Customer_ID AS Id FROM Customer_Account AS c) AS ac0
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_1
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 1) AS ac1
    ON ac0.id = ac1.id
  LEFT OUTER JOIN
       (SELECT c.Customer_ID AS Id, t.Type AS Account_Type_2
          FROM Customer_Account AS c
          JOIN AccountType      AS t ON c.Account_Type_ID = t.ID AND t.ID = 2) AS ac2
    ON ac0.Id = ac2.Id;