doufeng1249 2016-01-15 17:14
浏览 24
已采纳

如何将sql结果分成不同的html列

I have a table:

| 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 |

And result:

$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 table display:

+------------+------------+--------+-----+
| Date       | Movie      | Price  | Sum |
+------------+------------+--------+-----+
| 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 |

How to join results like this?

+------------+-------------+---------+---------+-----+    
| Date       | Movie       | Price_1 | Price_2 | Sum |
+------------+-------------+---------+---------+-----+  
| 2015-11-11 | The lord..  |  1 x 5  |  2 x 4  |  13 |
| 2015-11-11 | Harry..     |  2 x 13 |  2 x 5  |  36 |

Here is a msql example: http://sqlfiddle.com/#!2/2c0f4/2

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-01-15 17:35
    关注

    Naturally you can do it all in php and it's probably the best way to go about it.

    Now if you still want to do the most of it in the database you can do this

    UPDATED:

    SELECT s_date, s_movie, 
           GROUP_CONCAT(CONCAT(price_count, ' x ', s_price) ORDER BY s_price DESC) prices, 
           SUM(s_price * price_count) price_sum
      FROM
    (
      SELECT s_date, s_movie, s_price, COUNT(*) price_count, SUM(s_price) price_sum
        FROM table1
       GROUP BY s_date, s_movie, s_price
    ) q
     GROUP BY s_date, s_movie DESC
    

    Which will give you the following:

    +---------------------+-----------------------+--------------+-----------+
    | s_date              | s_movie               | prices       | price_sum |
    +---------------------+-----------------------+--------------+-----------+
    | 2015-11-11 00:00:00 | The lord of the rings | 1 x 5,2 x 4  |        13 |
    | 2015-11-11 00:00:00 | Harry Potter          | 2 x 13,2 x 5 |        36 |
    +---------------------+-----------------------+--------------+-----------+
    

    Here is SQL Fiddle demo

    Now you can explode() prices column value by , while you're iterating over the resultset.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀