我正在尝试对 MySQL(Ubuntu 上的 5.5.44-0)中的一些记录进行编号,按另一列分组(您将在下面明白我的意思)。我正在调整 Running Sums for Multiple Categories in MySQL 中描述的解决方案, 除了我只是编号,而不是求和。
涉及的表比较大,有将近100列,所以我们先简化演示,创建只包含重要列的派生表。抱歉没有共享 SQL Fiddle,因为它看起来不像是可复制的,除非处理大量数据,我无法共享:
创建表格:
CREATE TABLE `inquiries_test` (
`id` int(11) NOT NULL DEFAULT '0',
`motive` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`inquiry_id`),
KEY `motive` (`motive`)
);
insert into inquires_test select id, motive from inquiries;
CREATE TABLE `leads_test` (
`lead_id` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`inquiry_id` int(11) DEFAULT NULL,
KEY `id` (`lead_id`)
);
insert into leads_test select lead_id, created_at, inquiry_id;
CREATE TABLE `lead_inserts` (
`lead_id` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`cnt` int(11) DEFAULT NULL
);
您会注意到上面的 inquiries_test 和 leads_test 中的数据来自实际的生产表。其重要性将在稍后发挥作用。现在填充 lead_inserts:
playground>insert into lead_inserts (cnt, created_at, lead_id)
-> SELECT @cnt := if(@id = l.lead_id,@cnt,0) + 1 as cnt
-> , l.created_at
-> , @id := l.lead_id as local_resouce_id
-> FROM leads_test l join inquiries_test i on (l.inquiry_id=i.id)
-> CROSS JOIN (select @id := 0, @cnt := 0) as InitVarsAlias
-> where i.motive='real' ORDER BY lead_id, created_at;
Query OK, 2172774 rows affected (14.30 sec)
Records: 2172774 Duplicates: 0 Warnings: 0
playground>select * from lead_inserts where lead_id in (117,118);
+---------+---------------------+------+
| lead_id | created_at | cnt |
+---------+---------------------+------+
| 117 | 2012-06-23 00:13:09 | 1 |
| 117 | 2014-09-14 04:30:37 | 2 |
| 117 | 2015-01-27 22:34:41 | 3 |
| 117 | 2015-03-19 19:33:51 | 4 |
| 118 | 2014-12-24 17:47:15 | 1 |
| 118 | 2015-01-23 21:30:09 | 2 |
| 118 | 2015-04-07 21:33:43 | 3 |
| 118 | 2015-04-10 17:00:04 | 4 |
| 118 | 2015-05-12 21:59:49 | 5 |
+---------+---------------------+------+
到目前为止一切顺利 - 每个新 lead_id 的 cnt 值都会“重置”。既然 leads_test 和 inquiries_tests 基本上是删除了其他列的线索和查询,那么可以预期,如果我修改插入语句以使用原始表,结果应该是相同的,对吧?但是看:
playground>truncate table lead_inserts;
Query OK, 0 rows affected (0.14 sec)
playground>insert into lead_inserts (cnt, created_at, lead_id)
-> SELECT @cnt := if(@id = l.lead_id,@cnt,0) + 1 as cnt
-> , l.created_at
-> , @id := l.lead_id as local_resouce_id
-> FROM leads l join inquiries i on (l.inquiry_id=i.id)
-> CROSS JOIN (select @id := 0, @cnt := 0) as InitVarsAlias
-> where i.motive='real' ORDER BY lead_id, created_at;
Query OK, 2172774 rows affected (17.25 sec)
Records: 2172774 Duplicates: 0 Warnings: 0
playground>select * from lead_inserts where lead_id in (117,118);
+---------+---------------------+------+
| lead_id | created_at | cnt |
+---------+---------------------+------+
| 117 | 2012-06-23 00:13:09 | 1 |
| 117 | 2014-09-14 04:30:37 | 1 |
| 117 | 2015-01-27 22:34:41 | 1 |
| 117 | 2015-03-19 19:33:51 | 1 |
| 118 | 2014-12-24 17:47:15 | 1 |
| 118 | 2015-01-23 21:30:09 | 1 |
| 118 | 2015-04-07 21:33:43 | 1 |
| 118 | 2015-04-10 17:00:04 | 1 |
| 118 | 2015-05-12 21:59:49 | 1 |
+---------+---------------------+------+
编号发生了什么变化?使用原始表格时的其他观察结果:
那么,这是我遇到的错误,还是我遗漏了什么?在现实生活中,我不能使用上述过程作为解决方法 - 我真的必须使用线索和查询,因为这些表中的其他列必须是 lead_inserts 的一部分。
谢谢!
最佳答案
A Cha 指出,这看起来像是一个 MySQL 优化问题,当最终结果将被插入到一个新表中时,MySQL 发现没有理由执行 ORDER BY。为什么它适用于测试表而不适用于生产表,当它们具有相同的行数时,我不知道。但这就是我强制它对将要插入的内容进行排序的方式:
首先确保我将排序的列有一个连接索引:
CREATE INDEX idx_leads_lead_id_created ON leads(lead_id, created_at);
然后强制MySQL使用这个索引:
insert into lead_inserts (cnt, created_at, lead_id)
SELECT @cnt := if(@id = l.lead_id,@cnt,0) + 1 as cnt
, l.created_at
@id := l.lead_id as local_resouce_id
FROM leads l FORCE INDEX FOR ORDER BY (idx_leads_lead_id_created)
JOIN inquiries i on (l.inquiry_id=i.id)
CROSS JOIN (select @id := 0, @cnt := 0) as InitVarsAlias
WHERE i.motive='real'
ORDER BY lead_id, created_at;
关于MySQL 按组对记录进行编号 - 我遇到错误了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175090/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c