duanchuonong5370 2018-06-07 12:49
浏览 47
已采纳

PHP - 从数据库geting first record和其他3分钟值中选择数据

I just wanna know how to select data from specific user in database but just print data in a interval of 3 minutes.

For example: THIS IS DATABSE:

ID ------ User----- Hour----- Minute
#1 ------ USERA---- 09 ------ 00
#2 ------ USERA---- 09 ------ 01
#3 ------ USERA---- 09 ------ 02
#4 ------ USERA---- 09 ------ 03
#5 ------ USERA---- 09 ------ 04
#6 ------ USERA---- 09 ------ 05
#7 ------ USERA---- 09 ------ 06
#8 ------ USERA---- 09 ------ 07
#9 ------ USERA---- 09 ------ 08
#10 ------ USERA---- 09 ------ 09
#11 ------ USERA---- 09 ------ 10

THIS IS WHAT I WANNA SHOW

#1 ------ USERA---- 09 ------ 00
#4 ------ USERA---- 09 ------ 03
#7 ------ USERA---- 09 ------ 06
#10 ------ USERA---- 09 ------ 09
...

Is there a way to do this?

Thanks

  • 写回答

3条回答 默认 最新

  • douyouqian8550 2018-06-07 12:54
    关注

    A quick solution may be to use the modula function..

    SELECT * FROM table WHERE (table.Minute % 3) = 0

    If you want to start from the first instance of hour,minute and then get 3minutes from that, you may use this query:

    SELECT * FROM table WHERE ((table.Minute - (SELECT Minute FROM table ORDER BY table.Minute,table.Hour ASC LIMIT 1)) % 3 ) = 0;

    Working demo can be found here: https://paiza.io/projects/4tlF_ZtwTSmSHFY_YKhtaw?language=mysql

    Edit:
    You may get better results by using GROUP BY

    SELECT * FROM table WHERE ((table.Minute - (SELECT table.Minute FROM table GROUP BY table.Minute,table.Hour ORDER BY table.Hour ASC LIMIT 1)) % 3 ) = 0;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录