Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL将同一表中的多行合并到同一行中_Mysql_Sql_Join_Inner Join_Self Join - Fatal编程技术网

Mysql SQL将同一表中的多行合并到同一行中

Mysql SQL将同一表中的多行合并到同一行中,mysql,sql,join,inner-join,self-join,Mysql,Sql,Join,Inner Join,Self Join,我有两个表,表1(Customer)的行数有限,因此我希望能够将客户信息输入到此表(Customer)中,并跨两行保存,然后我希望能够读取这两行,并将它们合并到一行中,并使用SQL将其显示为一行 我能够正常地从表中读取数据,我正在将Customer数据库与CustomerDetails数据库链接,因为CustomerDetails包含显示Customer中的列所需的唯一值 SELECT c.Cust_4, c.Cust_5, C.Cust_6 //Selecting the rows to d

我有两个表,表1(Customer)的行数有限,因此我希望能够将客户信息输入到此表(Customer)中,并跨两行保存,然后我希望能够读取这两行,并将它们合并到一行中,并使用SQL将其显示为一行

我能够正常地从表中读取数据,我正在将Customer数据库与CustomerDetails数据库链接,因为CustomerDetails包含显示Customer中的列所需的唯一值

SELECT c.Cust_4, c.Cust_5, C.Cust_6  //Selecting the rows to display from Customer
FROM Customer C //From the Customer Database, as C
LEFT JOIN CustomerDetails CD ON C.Unique_val = CD.Unique_val. Joining the Customer DB with CustomerDetails DB, where the 'Unique_val' is the same.

INNER JOIN (SELECT Cust_40, MAX(CREATE_DT) AS MaxTime FROM Customers GROUP BY Cust_40)
grouped ON grouped.Cust_40 = C.Cust_40 //Taking the latest created entry in the Customer Database, Grouped by Cust_40, so it reads the most RECENT version

WHERE grouped.MaxTime = C.CREATE_DT //
AND CD.F_ID = 12332131 //The UNIQUE value inside of the CustomerDetails Database
结果:

c.Cust_4     c.Cust_5     C.Cust_6
 Info1         Info2        Info3
现在它工作得很好,它总是读取最新的行分组Cust_40,因此如果有10个条目Cust_40='davestrow',那么它将显示一行最新值的'davestrow'

现在,假设客户数据库的行数有限,所以我希望有20行,但它只有10行。 现在,我的目标是将客户数据库与自身“自连接”,读取两行或更多行,并将它们合并为同一行,其中某些值是相同的

这是一个两行的示例

Cust_4     Cust_5     Cust_6    Cust_40
 Info1      Info2      Info3    DavesRow


Cust_4     Cust_5     Cust_6    Cust_40
Info4       Info5      Info6    DavesRow
我的SQL尝试将这些行合并为一行:

SELECT c.Cust_4, c.Cust_5, c.Cust_6 d.Cust_4, d.Cust_5, d.Cust_6 //reading the same 3 rows, but from two different entries
    FROM Customer C 
    INNER JOIN Customers D ON D.Cust_39 = C.Cust_39 //SELF JOINING Customers ON Customers where CUST_39 is the same value

    LEFT JOIN CustomerDetails CD ON C.Unique_val = CD.Unique_val  // Joining the Customer DB with CustomerDetails DB, where the 'Unique_val' is the same.
    
    INNER JOIN (SELECT Cust_40, MAX(CREATE_DT) AS MaxTime FROM Customers GROUP BY Cust_40)
    grouped ON grouped.Cust_40 = C.Cust_40 //Taking the latest created entry in the Customer Database, Grouped by Cust_40, so it reads the most RECENT version
    
    WHERE grouped.MaxTime = C.CREATE_DT 
    AND grouped.MaxTime = D.CREATE_DT // THE CREATE_DT WILL BE THE SAME FOR EACH ROW 

    AND CD.F_ID = 12332131 //the CustomerDetails F_ID value will be different, it will be '12332131' for the 'C' instance of Customers, and '13424251' for the 'D' instance of Customers
    AND CD.F_ID = 13424251 
这不能正常工作。。。它在所有6行中显示c.Cust_4、c.Cust_5和c.Cust_6的值:c.Cust_4、c.Cust_5、c.Cust_6、d.Cust_4、d.Cust_5、d.Cust_6

结果

c.Cust_4     c.Cust_5     c.Cust_6    d.Cust_4    d.Cust_5    d.Cust_6   
  Info1       Info2       Info3        Info1       Info2       Info3   
期望结果

c.Cust_4     c.Cust_5     c.Cust_6    d.Cust_4    d.Cust_5    d.Cust_6   
  Info1       Info2       Info3        Info4       Info5       Info6
我希望我已经解释好了


任何帮助都将不胜感激。谢谢

示例数据和所需结果会有所帮助。没有一个表的行数是有限的。。