我有两张桌子:
表1: TBL_USD_AUD
+----+------------+----------+
| id | date | value |
+----+------------+----------+
| 1 | 2015-04-24 | 1.278400 |
+----+------------+----------+
| 2 | 2015-04-23 | 1.287200 |
+----+------------+----------+
| 3 | 2015-04-22 | 1.288300 |
+----+------------+----------+
| 4 | 2015-04-21 | 1.291500 |
+----+------------+----------+
| 5 | 2015-04-20 | 1.294300 |
+----+------------+----------+
表2: TBL_USD_EUR
+----+------------+----------+
| id | date | value |
+----+------------+----------+
| 1 | 2015-04-24 | 0.923873 |
+----+------------+----------+
| 2 | 2015-04-23 | 0.928333 |
+----+------------+----------+
| 3 | 2015-04-22 | 0.930839 |
+----+------------+----------+
| 4 | 2015-04-21 | 0.934579 |
+----+------------+----------+
| 5 | 2015-04-20 | 0.932575 |
+----+------------+----------+
如何选择这两个表:
$x = "SELECT date,rate FROM `currency`.`tbl_usd_aud` WHERE `date` >= (NOW() - INTERVAL 40 DAY)";
和:
$f = "SELECT date,rate FROM `currency`.`tbl_usd_eur` WHERE `date` >= (NOW() - INTERVAL 40 DAY)";
然后以某种方式我想在同一日期获得foreach值,得到以下等式:
if ($x['date'] == $f['date']){
echo "EUR-AUD: ".(1/$x['rate'])*$f['rate'];
}
答案 0 :(得分:2)
您也可以在数据库中进行计算:
SELECT a.date, a.rate x, e.rate f, e.rate/a.rate f_over_x
FROM tbl_usd_aud a
JOIN tbl_usd_eur e
ON e.date = a.date
WHERE a.date >= CURDATE() - INTERVAL 40 DAY
N.B。我更喜欢CURDATE()
到NOW()
,因为它涉及较少的隐式转换以及(1/x)*y = y/x
答案 1 :(得分:1)
这可能有助于你
SELECT a.date,a.rate,b.rate,(1/a.rate)*b.rate as eur_aud
FROM tbl_usd_aud a
LEFT JOIN tbl_usd_eur b on b.date = a.date
WHERE a.date >= (NOW() - INTERVAL 40 DAY)