0

我有 3 张桌子:

currency
id | name
1  | usd
2  | eur

currency_date
id |   date   | usd | eur
1  | 22.09.13 | 10  | 12
2  | 23.09.13 | 11  | 13
3  | 24.09.13 | 9   | 10

cost
id | id_currency | id_currency_date | cost
1  |     1       |       2          |  15

因此,对于表中id=1的行,cost我们有usd交换值11

是否可以通过不同表中其他字段的名称获取字段的值?

我认为它需要是这样的:

SELECT currency_date.VAR(currency.name) AS cur_val
FROM cost
LEFT JOIN currency ON currency.id = cost.id_currency
LEFT JOIN currency_date ON currency_date.id = cost.id_currency_date
WHERE cost.id = 1

currency_date.VAR(currency.name)只是为了显示我想要的。

4

1 回答 1

2

这应该这样做。

SELECT case currency.name when 'usd' then currency_date.usd when 'eur' then currency_date.eur end AS cur_val
FROM cost
LEFT JOIN currency ON currency.id = cost.id_currency
LEFT JOIN currency_date ON currency_date.id = cost.id_currency_date
WHERE cost.id = 1
于 2013-09-26T14:15:12.840 回答