dongziduo9762 2013-01-04 02:56
浏览 295
已采纳

将一行mysql表中的行复制到另一个表,并修改字段值

I have a tabledaily and a tablefood. In food, I store a database of foods I eat into food. In daily, I only store foods I eat for the day (must be selected from food, modified, and stored in daily), and at the end of the day, the daily table is purged (all rows dropped with DELETE FROM daily).

What I'm trying to figure out is how I can copy one row from table food and copy it to table daily while modifying it's values.

Both food and daily have an identical structure.

food (nothing in this table ever changes)

name    calories   grams  fat   protein
---------------------------------------
apple   50         80     1.2   2
potato  90         45     3.4   5
carrot  10         70     6.2   6

daily (the table after I ate an apple that is 93g as per example)

name    calories   grams  fat   protein
---------------------------------------
apple   58.125     93     1.395 2.325

example

I weigh out a food that only exists in my food database, and if it doesn't, I'll have to add it in.

I then pick a food from a dropdown and enter it's weight in grams.

I choose from the list an apple. I weigh my apple and it is 93g.

My 93g is divided by 80g which yields 1.1625. All the stats for the apple (fat and protein) have to be multiplied by 1.1625 and then inserted into my daily table.

How can I retrieve a row from food, modify every value in it by my modifier (1.1625), and insert it into my daily table?


I just realized that I can load up whichever food I select into an array and modify each cells values, then pump that array into daily.

  • 写回答

2条回答 默认 最新

  • doudeng2025 2013-01-04 03:15
    关注

    In PHP you would just load the row, modify the values and build a query to insert it into daily. Nothing fancy there, so I guess you are asking about how to do it in SQL:

    INSERT INTO daily (name,
                       calories,
                       grams,
                       fat,
                       protein)
    SELECT name,
           calories * 1.1625,
           grams * 1.1625,
           fat * 1.1625,
           protein * 1.1625
    FROM food
    WHERE name = "apple"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置