0

次の例のテーブルが与えられます。

 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;

おそらくそこにいくつかのタイプミスがありますが、それはコンセプトを示していると思います. これを行うより良い方法はありますか?

4

3 に答える 3

0

を使用してみてくださいUNION ALL

SELECT user_id, value, date_activated FROM CREDITS
UNION ALL
SELECT user_id, value, date_activated FROM EXPENSE

そして、あなたは2番目の選択を読んで欲しいかもしれません:

SELECT user_id, value * -1, date_activated FROM EXPENSE、クレジットと費用の間で正しい計算を行うことができるようにします。クレジットと経費の区別をさらに支援するコードを追加することもできます。

SELECT user_id, value, date_activated, 'c' as `type` FROM CREDITS
UNION ALL
SELECT user_id, value * - 1, date_activated,'e' as `type` FROM EXPENSE
于 2013-01-24T08:56:22.493 に答える
0

をご覧くださいUNION MYSQL CLAUSE 。それだ。

(SELECT a FROM t1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 ORDER BY a LIMIT 10);
于 2013-01-24T08:51:43.070 に答える
0

これを試して:

    SELECT user_id, value,date
    FROM
      ( SELECT id,
               user_id,
               value,
               date_activated AS date
       FROM credits
       UNION ALL SELECT id,
                    user_id,
                    value,
                    date_spend AS date
       FROM expense ) x
    ORDER BY id, date; 
于 2013-01-24T08:59:11.440 に答える