mysql组通过使用范围记录值
在 SQL 中,我有下面的表 t_test:
emp_code | period_month | company_code NIK001 | 01 | ALPHA NIK001 | 02 | ALPHA NIK001 | 03 | ALPHA NIK001 | 04 | ALPHA NIK001 | 05 | ALPHA NIK001 | 06 | BETA NIK001 | 07 | BETA NIK001 | 08 | BETA NIK001 | 09 | BETA NIK001 | 10 | ALPHA NIK001 | 11 | ALPHA NIK001 | 12 | ALPHA
我想查询以下结果:
emp_code | company_code | from_month | to_month -------------------------------------------------- NIK001 | ALPHA | 01 | 05 NIK001 | BETA | 06 | 09 NIK001 | ALPHA | 10 | 12
In SQL I have table t_test below:
emp_code | period_month | company_code NIK001 | 01 | ALPHA NIK001 | 02 | ALPHA NIK001 | 03 | ALPHA NIK001 | 04 | ALPHA NIK001 | 05 | ALPHA NIK001 | 06 | BETA NIK001 | 07 | BETA NIK001 | 08 | BETA NIK001 | 09 | BETA NIK001 | 10 | ALPHA NIK001 | 11 | ALPHA NIK001 | 12 | ALPHA
I want to query with result below:
emp_code | company_code | from_month | to_month -------------------------------------------------- NIK001 | ALPHA | 01 | 05 NIK001 | BETA | 06 | 09 NIK001 | ALPHA | 10 | 12
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这让我恐惧地颤抖,但它确实输出了你所追求的近似值:
但是,我只是出于好奇而尝试了这个。您不应该在生产环境中真正依赖类似的东西。我认为简单的 SQL 查询不太适合您的问题。我会去别处看看。
编辑:基于下表:
填充以下 INSERT 语句:
This makes me tremble in fear, but it does output an approximation of what you're after:
However, I tried this just out of curiosity. You shouldn't really rely on anything like that for your production environment. I don't think that a simple SQL query is a good fit for your problem. I would look elsewhere.
EDIT: Based on the following table:
Filled with the following INSERT statements:
您可以使用 MySQL 变量。例如:
在我的测试数据上,这给了我:
这个想法是在每一行检查当前的company_code是否等于前一条记录的company_code。如果不同,则增加变量。
这是向前迈出的一步。但如何对行进行分组呢?您无法直接对它们进行分组,但需要用第二个查询包装该查询。
这是在外部查询中使用内部查询的结果。对于我的测试数据,
如果您需要限制行的选择,请在 内部 查询的 where 子句中执行此操作,否则整个数据库将在每个查询上加载到内存中。另一个问题是,内部查询将前一条记录作为“具有最接近的较低
period_month
的记录。因此,您不能按任何内容对内部查询进行排序但是period_month ASC
,如果您愿意,您仍然可以在外部查询中对内容进行排序。最后,如果您使用旧的
mysql_query
接口。出于某种原因,你需要把SET
行在一个单独的查询中,因为它一次只能处理一个查询,不确定这种方法在扩展到大型数据库时效果如何,但它肯定有效。 “方式。只要确保您不会意外地弄乱查询中的某些内容,以防您不完全理解它。:)
You could use a MySQL variable. Eg:
On my test data this gives me:
The idea is that on every line you check if the current
company_code
is equal to that of the previous record. If it's different, increase the variable.That's a step forward. But how to group the rows? You can't group them directly, but you need to wrap that query with a second one.
That's using the result from the inner query, in the outer query. This gives me, with my test data,
If you need to limit the selection of rows, do so in a where clause in the inner query, or else the whole database will be loaded into memory on each query. Another gotcha is that the inner query is fetching the previous record as "the record that has the closest lower
period_month
. As an effect, you must not sort the inner query by anything butperiod_month ASC
. You can still sort things in the outer query if you want.Finally, if you're using the old
mysql_query
interface for some reason, you need to put theSET
line in a separate query, as it can only handle one query at a time.Not sure how well this method works out when scaled up to large databases, but it definitely works. Maybe there's a "good" way. Just make sure you don't accidentally mess something up in the query, in case you don't fully understand it. :)
@magma:非常感谢您的回答。好工作!谢谢..
我只是做了一点修改,所以 from_month 和 to_month 永远不会得到 NULL 值。
为了加快执行查询的速度,我使用会话创建临时表来存储查询结果,并使用临时数据来生成报告
@magma : thnks a lot for the answer. good job! thanks..
I just made a little modification so the from_month and to_month will never get NULL value.
To speed up when execute query, i create temporary table with session to store the query result, and use the temporary data for producing report