mysql - SQL查询查看差异值

标签 mysql sql

我每天凌晨 4 点对用户登录日志(登录或未登录)进行每日快照,按每个地区和每日用户的总和进行分类。 但是,我想创建一个 SQL 查询来查看与前一天的差异值(可能是百分比),以便我可以看到哪个区域增加或减少了他们的用户。 我知道我可以对此进行硬编码,但是否有任何优雅的方法可以在相当简单的 SQL 查询中做到这一点?

最佳答案

There are two ways to do this:
First Way:
1. Create a table in your database
    CREATE TABLE user_logs (id int(11) AUTO_INCREMENT, daily_number varchar(1000), current_percentage varchar(30), date datetime)
2. Run the script to get the number of users for that day
    INSERT INTO user_logs (daily_number) VALUES ($daily_value)
3. Create function to compare the latest two DB rows, then insert percentage into (numberOfRows-1)


Second Way:
1. Create a log of the user records in a txt file
2. In the log, append each number seperated by comma
3. Create function to compare the last two values in log and take the average of the two

关于mysql - SQL查询查看差异值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614062/

相关文章:

PHP导出DBF文件格式

php - 如何使表格列可排序

mysql - 如何根据时间戳查询每条记录的索引

SQL 移动平均线

sql - Access 查询在 "select"模式下运行顺利,在 "make table"模式下失败

mysql - 如果第一个查询没有生成结果,如何在 mysql 上运行两个查询

php - 如何循环mysql_query直到结果?

mysql - 如何使用 Windows 任务计划程序导入 CSV 或使用命令提示符计划任务

mysql - 关于 Druid 中查询的用例

mysql - 优化自定义 Wordpress SQL 查询以获取用户元数据