如何组合连接表结果中的列?

时间:2015-08-30 11:16:19

标签: mysql database select join

我有一个看起来像这样的mysql表:

表PaymentType

ID   PaymentCode   PaymentName
1      P1        CASH
2      P2        TRANSFER
3      P3        CREDIT CARD

表人

ID   ID_PERSON   PersonName
1      123        GEORGE
2      124        HENRY
3      125        MICHAEL
4      126        ANNA

表INVOICE_IN

ID   ID_INVOICE_IN   Person_ID  Amount PaymentType_ID
1      II-001            1       100      1
2      II-002            2       200      1
3      II-003            4       300      2

表INVOICE_OUT

ID   ID_INVOICE_OUT   Person_ID  Amount     PaymentType_ID
1      IO-001                 1       500      2
1      IO-002                 3       600      3

我想要这样的结果:

ID_INVOICE_IN PaymentCode  PersonName  Amount ID_INVOICE_OUT PaymentCode  PersonName  Amount
II-001            P1         George     100        IO-001     P2          George      500 
II-002            P1         Henry      200        IO-002     P3       Michael       600 
II-003            P2         Anna       300         NULL      NULL       NULL      NULL

我有这样的查询:

SELECT INVOICE_IN.ID_INVOICE_IN, PaymentType.PaymentCode, PERSONS.PersonName, INVOICE_IN.Amount FROM INVOICE_IN LEFT JOIN PaymentType ON PaymentType.id = INVOICE_IN.PaymentType_ID LEFT JOIN PERSONS ON PERSONS.id = INVOICE_IN.Person_ID

该查询成功显示了4列 for invoice_in

我也有这样的查询:

SELECT INVOICE_OUT.ID_INVOICE_OUT, PaymentType.PaymentCode, PERSONS.PersonName, INVOICE_OUT.Amount FROM INVOICE_OUT LEFT JOIN PaymentType ON PaymentType.id = INVOICE_OUT.PaymentType_ID LEFT JOIN PERSONS ON PERSONS.id = INVOICE_OUT.Person_ID

查询成功显示了4列 for invoice_out

我需要在result1(invoice_in)和result2(invoice_out)之间进行组合,因此查询结果有8列。

2 个答案:

答案 0 :(得分:1)

如果我理解你的话,你想要做的就是向彼此相邻的不相关数据集展示。

执行此操作的一种方法是对每个集合中的行进行编号,并使用较大的集合作为左侧进行左连接。 (更好的方法是使用full outer join,但MySQL不支持,尽管使用左/右连接w / union可以实现相同的效果。使用MySQL,您可以使用用户变量对行进行编号。

这个查询给出了你的例子的结果,虽然我没有真正看到并排显示无关数据的意义。 (如果真的不相关......问题中没有任何内容表明它是相关的。)

select 
  t1.id_invoice_in, t1.paymentcode, t1.personname, t1.amount,
  t2.id_invoice_out, t2.paymentcode, t2.personname, t2.amount  
from (
  select     
    id_invoice_in, paymentcode, personname, amount,
    (@r1 := @r1 + 1) as r1
  from invoice_in 
  join paymenttype pt on pt.id = paymenttype_id 
  join persons p on p.id = person_id
  ,(select @r1 := 0) r
) t1 left join (
  select 
    id_invoice_out, paymentcode, personname, amount,
    (@r2 := @r2 + 1) as r2
  from invoice_out 
  join paymenttype pt on pt.id = paymenttype_id 
  join persons p on p.id = person_id
  ,(select @r2 := 0) r
) t2 on t1.r1 = t2.r2;

Sample SQL Fiddle

答案 1 :(得分:-2)

SELECT 
    `In`.`ID_INVOICE_IN`, 
    `PayI`.`PaymentCode`, 
    `PersI`.`PersonName`, 
    `In`.`Amount`, 
    `Out`.`ID_INVOICE_OUT`, 
    `PayO`.`PaymentCode`, 
    `PersI`.`PersonName`, 
    `Out`.`Amount` 
FROM
    `INVOICE_IN`  AS `In`,
    `INVOICE_OUT` AS `Out`,
    `PERSONS`     AS `PersI`,
    `PERSONS`     AS `PersO`,
    `PaymentType` AS `PayI`,
    `PaymentType` AS `PayO`
WHERE
    `In`.`ID`              = `Out`.`ID`   AND
    `In`.`Person_ID`       = `PersI`.`ID` AND
    `Out`.`Person_ID`      = `PersO`.`ID` AND
    `In`.`PaymentCode_ID`  = `PayI`.`ID`  AND
    `Out`.`PaymentCode_ID` = `PayO`.`ID`
ORDER BY
    `In`.`ID_INVOICE_IN`