dousi6405 2016-08-09 08:33
浏览 14
已采纳

替换表名并打印每列的所有颜色

Here's my query:

mysql>

select DATE_FORMAT(STR_TO_DATE(items.date, '%Y-%m-%d'), '%M %d, %Y') date_new, unit, Descpt, p_cost, add_by, GROUP_CONCAT(color,' = ',qty) as COLOR, SUM(qty) as Total, SUM(qty*(p_cost)) as Ptotal from items where status IN ('1','2') Group By Descpt;
 +---------------+------+------------------------+--------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+-----------+
 | date_new      | unit | Descpt                 | p_cost | add_by | COLOR                                                                                                                                                                             | Total | Ptotal    |
 +---------------+------+------------------------+--------+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------+-----------+
 | June 16, 2016 | Pcs  | #1688N - Big Crate New | 125.00 | Psd    | BLUE - W = 200,BLUE - W/O = 400,RED - W/O = 300,RED - W = 500,GREEN - W = 300,GREEN - W/O = 200,YELLOW       - W = 400,YELLOW        - W/O = 582,BLACK - W = 255,BLACK - W/O = 330 |  3467 | 900927.75 |

Color values will be print as column. how can I achieve it by select statement? attached is the needed output.

enter image description here

  • 写回答

1条回答 默认 最新

  • dops57958 2016-08-09 10:41
    关注

    This is not the complete answer, but I believe it will show you the way...

    The ideia is to get the COLOR column into the PHP realm and them make some regular expressions to get the data... after that, draw the table as you wish...

    $color = "BLUE - W = 200,BLUE - W/O = 400,RED - W/O = 300,RED - W = 500,GREEN - W = 300,GREEN - W/O = 200,YELLOW       - W = 400,YELLOW        - W/O = 582,BLACK - W = 255,BLACK - W/O = 330";
    
    if(preg_match_all("/(\w+)\s*\-\s*(\S+)\s*=\s*(\d+)/",$color,$m)) {
      print_r($m);
    }
    else {
      print "Regexp failed!
    ";
    }
    

    array $m will have all the data you need separated by lines and columns.

    Note that the regular expression may need some adjustements if the data differs for other results, for example if some numbers are missing somewhere...

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?