duanchuang1935 2015-10-26 20:37
浏览 34

通过使用php拆分为N个数组来组合mysql数据库记录

I have a mysql database with thousands of rows, each row contains a data for a sale transaction for one of the products. These products have unique IDs and total number of products is undefined.

ID      Product Name     Transaction ID     Date

123   Product 1    ergkljdlkjfgklgh    Sept 1, 2015
144   Product 5    eyghfeoopjfgkl    Sept 5, 2015
123   Product 1    jdlkjfghrttgklgej    Sept 8, 2015
144   Product 5    ngfukhjkgfhjjjcs    Sept 15, 2015
123   Product 1    utebhjgdsdhjfdt    Sept 20, 2015

and so on.

With help of php, I need to convert database into a simple list with each row containing unique product ID, product name, number of occurrences of that product in database, and sale dates joined into one string (that will be processed later).

To illustrate, the table above should convert into:

ID      Product Name     Number of Sales     Dates

123   Product 1    3    Sept-1-2015_Sept-8-2015_Sept-20-2015
144   Product 5    2    Sept-5-2015_Sept-15-2015

I think the way to do it has to do with sorting and looping through different arrays, however I am a bit confused and cant quite wrap my mind around it.

Do I sort by ids, get all unique ids into array, loop through array to retrieve the groups and numbers of items, make a separate array for each group to get the dates?

Maybe someone who've encountered such challenge before can point me into the right direction :)

  • 写回答

1条回答 默认 最新

  • doulu0266 2015-10-26 21:00
    关注

    Do you want to create new table or work on the same one. If its a different one then its simple that you do a select using on the columns and then order them using the ID. To get the dates you can use COALESCE(field1, '_') || COALESCE(field2, ''). Else you can handle that programmatically in PHP by having a global variable that holds the value of date and then for each instance of the product append the dates using the dot operator.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题