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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料