给定以下示例表。
CREDITS
+-------+--------------+--------------+---------------------------+
| ID | userid | value | date_activated |
+-------+--------------+--------------+---------------------------+
| 1 | 1722 | 50 | 2012-11-08 22:24:41 |
+-------+--------------+--------------+---------------------------+
| 2 | 3242 | 10 | 2012-11-07 22:24:41 |
+-------+--------------+--------------+---------------------------+
| 3 | 23232 | 20 | 2012-11-06 22:24:41 |
+-------+--------------+--------------+---------------------------+
| 4 | 1722 | 30 | 2012-11-19 22:24:33 |
+-------+--------------+--------------+---------------------------+
EXPENSE
+-------+--------------+--------------+---------------------------+
| ID | userid | value | date_spent |
+-------+--------------+--------------+---------------------------+
| 1 | 1722 | 10.20 | 2012-11-18 22:24:41 |
+-------+--------------+--------------+---------------------------+
| 2 | 3242 | 2.00 | 2012-11-03 22:24:41 |
+-------+--------------+--------------+---------------------------+
| 3 | 23232 | 20.00 | 2012-11-01 22:24:41 |
+-------+--------------+--------------+---------------------------+
| 4 | 1722 | 18.00 | 2012-11-20 22:24:33 |
+-------+--------------+--------------+---------------------------+
我想达到以下结果。
| 1722 | 50 | 2012-11-08 22:24:41 |
| 1722 | 10.20 | 2012-11-18 22:24:41 |
| 1722 | 30 | 2012-11-19 22:24:33 |
| 1722 | 30 | 2012-11-19 22:24:33 |
| 1722 | 18.00 | 2012-11-20 22:24:33 |
| 3242 | 10 | 2012-11-07 22:24:41 |
| 3242 | 2.00 | 2012-11-03 22:24:41 |
| 23232 | 20.00 | 2012-11-01 22:24:41 |
| 23232 | 20.00 | 2012-11-01 22:24:41 |
我通常会如何做这将是做类似下面的事情
$query = "SELECT * FROM CREDITS ORDER BY user id";
$result = mysql_query($query);
while ($row = mysql_fetch_array( $result ))
{
$combind_result = $combind_result.$row['userid']."|".$row['value']."|".$row['date_activated']."|";
$user_id = $row['user_id'];
$query2 = "SELECT * FROM EXPENSE userid = '$user_id' ORDER BY date_spent";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_array( $result2 ))
{
$combind_result.$row2['userid']."|".$row2['value']."|".$row2['spent']."|";
}
}
echo $combind_result;
可能有一些错别字,但我认为它显示了这个概念。有一个更好的方法吗?