问题描述
我有一个标题为"psytable_moist_air"的表,如下所示:
I have a table titled "psytable_moist_air" shown below:
我正在尝试开发一个MySQL语句,该语句将插入可能在记录之间的值. (如果可以更轻松地用PHP进行数学运算,我也欢迎该解决方案!]
I'm trying to develop a MySQL statement that will interpolate a value that may be between records. ( If it is easier to do the math in PHP I'm open for that solution too!]
示例:我想知道其中"T" = 17.8的"hda"值.
Example: I would like to know the "hda" value where "T" = 17.8.
请注意,没有记录,其中"T" = 17.8.但是,由于该表是线性相关的,因此我可以获得"T" = 17的"hda"值和"T" = 18的"hda"值,并对这些值进行线性插值.
Notice there is no record where "T"=17.8. However, because this table is linearly related, I can get the "hda" value where "T"=17 and the "hda" value where "T"=18 and do a linear interpolation of the values.
结果数学将如下所示:
(获取"hda"值,其中T = 17:hda = 17.102)
(获取"hda"值,其中T = 18:hda = 18.108)
The result math would look like this:
(Get the "hda" value where T=17 : hda = 17.102)
(Get the "hda" value where T=18 : hda = 18.108)
编辑:我能想到的唯一方法是执行两个MySQL语句以获取越来越小的值:
EDIT:The only way I can think of is to do two MySQL statements to grab the smaller and larger values:
SELECT MAX(`T`), MAX(`hda`) FROM `psytable_moist_air` WHERE `T`<17.8
SELECT MIN(`T`), MIN(`hda`) FROM `psytable_moist_air` WHERE `T`>17.8
然后,我将使用这两个值进行插值.这行得通,但似乎效率很低.谁能提出更好的解决方案?
Then I would use these two values to do the interpolation. This works but seems pretty inefficient. Can anyone come up with a better solution??
如果有人有任何建议,将不胜感激!
If anyone has any advice it would be much appreciated!
.
推荐答案
我认为最好的方法是使用floor函数.
I believe the best way to do this would be to use the floor function.
请参阅以下内容;
SET @x = 17.8;
SET @x0 = FLOOR(@x);
SET @x1 = FLOOR(@x) + 1;
SET @y0 = (SELECT `hda` FROM `psytable_moist_air` WHERE `T` = @x0);
SET @y1 = (SELECT `hda` FROM `psytable_moist_air` WHERE `T` = @x1);
SET @y = @y0 + (@y1 - @y0) * ((@x - @x0) / (@x1 - @x0));
Select @y ,@x0 ,@x1, @y0 ,@y1;
这篇关于MySQL的PHP线性插值表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!