douqin7086 2013-09-19 16:26
浏览 18
已采纳

如何在MySQL中获取线性函数行?

I have a MySQL table with 1000 rows. And I want to get 4 specific rows based on linear function as step (get value of row, last position must be the last row, row number 1000). Example:

---------------------
 id  | value
---------------------
  1  | 3
  2  | 1
  3  | 5
  4  | 4
 ... | ...
 997 | 2
 998 | 0
 999 | 7
1000 | 4
--------------------

Step as linear function (1,2,3....,N)

*) step=1  =>  i will get 4 rows of id : 997,998,999,1000   
       first row to get is row number 997, last row is row number 1000
       result = 2,0,7,4
*) step=2  =>  i will get 4 rows of id : 994,996,998,1000
*) step=3  =>  i will get 4 rows of id : 991,994,997,1000
*) step=4  =>  i will get 4 rows of id : 988,992,996,1000
*) step=5  =>  i will get 4 rows of id : 985,990,995,1000
.....
*) step=330 => i will get 4 rows of id : 10,340,670,1000
*) step=331 => i will get 4 rows of id : 7,338,669,1000
*) step=332 => i will get 4 rows of id : 4,336,668,1000
*) step=333 => i will get 4 rows of id : 1,334,667,1000

How to do it in PHP?

  • 写回答

1条回答 默认 最新

  • drju37335 2013-09-19 16:55
    关注

    Considering you want last row to always be id 1000:

    function linear($step) {
    
     $query = "SELECT * FROM TABLE WHERE id IN (1000, 1000 - $step, 1000 - $step * 2, 1000 - $step * 3)"
     $result = mysql_query($query)
    }
    

    In case you do not know what is the last ID:

    function linear($step) {
     $lastId = mysql_fetch_assoc(mysql_query("SELECT id FROM TABLE ORDER BY id DESC LIMIT 1"))['id']
     $query = "SELECT * FROM TABLE WHERE id IN ($lastId, $lastId - $step, $lastId - $step * 2, $lastId - $step * 3)"
     $result = mysql_query($query)
    }
    

    Feel free to ask if you need more information. You are welcome.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统