不同的值,总和值,然后加入结果

时间:2016-01-14 11:36:51

标签: php mysql

我有一张桌子:

| s_id | s_date     | s_movie               | s_hour | s_price |
|    1 | 2015-11-11 | The lord of the rings |  11:00 |       5 |
|    2 | 2015-11-11 | The lord of the rings |  11:00 |       4 |
|    3 | 2015-11-11 | The lord of the rings |  11:00 |       4 |
|    4 | 2015-11-11 | Harry Potter          |  12:00 |       5 |
|    5 | 2015-11-11 | Harry Potter          |  12:00 |      13 |
|    6 | 2015-11-11 | Harry Potter          |  12:00 |      13 |
|    7 | 2015-11-11 | Harry Potter          |  12:00 |       5 |

结果:

$result = mysql_query("
    SELECT *
     , count(s_price) as uniq_p 
     , SUM(s_price) as uniq_all 
    from table 
    group 
    by s_movie
     , s_price 
    order 
    by s_movie desc
");

HTML表格显示:

<b>Date  Movie  Price  Sum</b>
2015-11-11 The lord..   2 x 4   8
2015-11-11 The lord..   1 x 5   5
2015-11-11 Harry..  2 x 5   10
2015-11-11 Harry..  2 x 13  26

如何加入这样的结果?

<b>Date  Movie  Price_1 Price_2  Sum</b>
2015-11-11 The lord..   2 x 4 1 x 5  13
2015-11-11 The Harry..2 x 5 2 x 13 36

这是一个msql示例: http://sqlfiddle.com/#!2/2c0f4/2

和PHP代码:

<table border="1" style="width:50%">
<thead>
<tr>
    <td>Date</td>
    <td>Movie</td>
    <td>Price</td>
     <td>Sum</td>
  </tr>
</thead>
 <?  while ($row = mysql_fetch_array($result)):?>

  <tr>
    <td><?= $row["s_date"]; ?></td>
    <td><?= $row["s_movie"]; ?></td>
    <td><?= $row["uniq_p"]; ?> x <?= $row["s_price"]; ?></td>
     <td><?= $row["uniq_all"]; ?></td>
  </tr>


<? endwhile ?>
</table>

0 个答案:

没有答案