用于更新百分比变化的 SQL 语句
我已经搜索了这个答案,并且已经接近答案,但仍然不够接近。我很想知道 MySQL 是否有这个功能。
我已经使用 Perl 和 MySQL 4 进行开发,现在使用 MySQL 4。 我的表看起来像这样...
- symbol varchar(25)
- TodayDate
- dateinterest int(11)
我的问题是......这些符号(大约 200,000 个)每天都会更新为兴趣字段的新数字。
一个例子是这样......
symbol | todayDate | interest
-------------------------------
A202015 | 2010-10-26 | 150
A202015 | 2010-10-25 | 100
理想情况下,我能够做的是在最后更新另一个字段,并与前一个记录相比发生百分比变化。上面的内容看起来像这样......
symbol | todayDate | interest | change
-----------------------------------------
A202015 | 2010-10-26 | 150 | 50
A202015 | 2010-10-25 | 100
我不认为这个功能在 MySQL 中是可能的。我得出的结论是,我只需要获取以前的记录信息,进行数学计算,然后用百分比信息更新最新记录。我只是想再仔细检查一下,看看是否有 MySQL 天才有智慧来通过我的道路。
I have searched S.O. for this answer and have came close to an answer but still not close enough. I am interested in knowing if MySQL has this capability.
I have developed in Perl and MySQL 4 and I'm on MySQL 4 now.
My table looks like this...
- symbol varchar(25)
- todayDate date
- interest int(11)
My problem is this.....these symbols (about 200,000 of them) update everyday with a new number for the interest field.
An example would be this....
symbol | todayDate | interest
-------------------------------
A202015 | 2010-10-26 | 150
A202015 | 2010-10-25 | 100
Ideally what I would be able to do would be to update another field at the end with a percentage change from the previous record. The above would then look like this....
symbol | todayDate | interest | change
-----------------------------------------
A202015 | 2010-10-26 | 150 | 50
A202015 | 2010-10-25 | 100
I didn't think that this functionality was possible in MySQL. I have come to the conclusion that I just need to grab the previous record info, do the math and then update the latest record with the percentage info. I just thought I would double check and see if any MySQL geniuses had any wisdom to pass my way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在与 Wilkie 女士通过电子邮件交谈后,结果发现她想要这样的百分比变化:
After an email conversation with Ms. Wilkie, it turns out she wanted a percent change like this:
由于 MySQL 引用子查询的方式,这有点奇怪,但这将满足我的需求:
这会导致:
哦,我应该补充一下,这是针对 mysql 5.x 数据库服务器的。我不确定它是否适用于 4.x,因为我没有 4.x 服务器可供测试,抱歉。
-大学教师
Its a little weird because of the way MySQL references sub-queries, but this will do what you need i think:
this results in:
oh, i should add, this is against a mysql 5.x database server. i'm not sure if it will work against 4.x as i don't have a 4.x server to test with, sorry.
-don
从示例数据中,我假设记录没有“更新”,而是正在插入新记录。
($interest 和 $symbol 是包含您要插入的值的变量,
rates
是表的名称 - 替换为实际值)From the sample data I assume the records are not "updating" but rather new records are being inserted.
($interest and $symbol are variables containing the values you are inserting,
rates
is the name of the table - replace with the actual values)