[英]Copying a row from one mysql table to another table, and modifying field values
I have a table daily
and a table food
. 我
daily
都有桌子和餐桌food
。 In food
, I store a database of foods I eat into food
. 在
food
,我保存的食品数据库我吃进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
). 在
daily
,我只存储我当天吃的食物(必须从food
选择,修改并daily
储存),并且在一天结束时, daily
餐桌被清除(所有行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. 我试图找出是如何从表中复制一个行
food
,并将其复制到餐桌daily
在修改它的值。
Both food
and daily
have an identical structure. food
和daily
food
都有相同的结构。
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) 每天 (我吃了一个93克的苹果后的表格)
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
. 我从列表中选择一个
apple
。 I weigh my apple
and it is 93g
. 我称我的
apple
为93g
。
My 93g
is divided by 80g
which yields 1.1625
. 我
93g
除以80g
这将产生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. apple
(脂肪和蛋白质)的所有统计数据必须乘以1.1625
,然后插入我的daily
表格中。
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? 如何从
food
检索一行,通过我的修饰符( 1.1625
)修改其中的每个值,并将其插入我的daily
表中?
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
. 我刚刚意识到我可以将我选择的任何食物加载到一个数组中并修改每个单元格值,然后将该数组泵入
daily
。
In PHP you would just load the row, modify the values and build a query to insert it into daily
. 在PHP中,您只需加载行,修改值并构建查询以将其插入到
daily
。 Nothing fancy there, so I guess you are asking about how to do it in SQL: 没有什么花哨的,所以我想你问的是如何在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"
Can I suggest a slight modification to your schema that might make your life simpler? 我是否可以建议对您的架构稍作修改,以使您的生活更简单? With the one you have, your data is not normalized.
使用您的数据,您的数据不会正常化。
Change your daily table to contain only two columns, one for the name and one for the grams, and this is the only information you need to insert, no need to modify any values from the food table. 将每日表更改为仅包含两列,一列用于名称,另一列用于克,这是您需要插入的唯一信息,无需修改食物表中的任何值。
Now, to retrieve the values you want to display daily, do this: 现在,要检索要每天显示的值,请执行以下操作:
SELECT d.name, d.grams, f.fat*(d.grams/f.grams), f.protein*(d.grams/f.grams)
FROM daily d, food f
WHERE f.name=d.name;
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.