MySQL查询反转数据

时间:2015-07-03 08:28:47

标签: mysql

我的MySQL查询有问题。我的数据看起来像这样:

transaction_type | currency_from | currency_to |
------------------------------------------------
swap             | USD           | IDR         |
swap             | IDR           | USD         |
forward          | GBP           | USD         |
forward          | USD           | GBP         |
forward          | JPY           | EUR         |
forward          | EUR           | JPY         |
spot             | USD           | EUR         |
spot             | EUR           | USD         |

是否可以让输出看起来像这样:

transaction_type | currency_from | currency_to |
------------------------------------------------
swap             | USD           | IDR         |
forward          | GBP           | USD         |
forward          | JPY           | EUR         |
spot             | USD           | EUR         |

如上所述,每笔交易都会有逆转交易,掉期美元兑换IDR,也会有掉期IDR-USD远期英镑兑美元也会有远期美元兑英镑,所以做现货。我尝试了很多方法,但仍然没有成功。有人可以帮忙吗?在此之前谢谢..

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作

SELECT t1.*
FROM tblTransaction AS t1 INNER JOIN tblTransaction AS t2
ON (
    t1.transaction_type = t2.transaction_type AND 
    t1.currency_from = t2.currency_to AND
    t1.currency_to = t2.currency_from AND
    t1.currency_from > t2.currency_from
)