I want these lines to run FOR ALL of the values of my database [duplicate]

爷,独闯天下 提交于 2019-12-11 16:52:01

问题


 $sql = 'SELECT * FROM t1
         RIGHT OUTER JOIN t2 ON t2.wid = t1.wid
         LIMIT ' . $number . ' OFFSET 678';

When i change by my hand this offset it runs for different values of my database... How I can do it without change with my hand this offset?? Someone said me that I can do it without offset but he did not tell me how... Could someone help plz?? I have over a month with this issue :'( :'( :'(


回答1:


$per_page = 10;
$current_page = read_current_page(); // 0,1,2...
$start = $current_page*$per_page;

 $sql = 'SELECT * FROM t1
         RIGHT OUTER JOIN t2 ON t2.wid = t1.wid
         LIMIT ' . $start . ','. $per_page;
save_current_page(++$current_page);

It should be something like that. read_current_page() should somehow read value from file, database or where ever you decide to keep that value. So, you need some place to store it between the calls.

Then you create your query and at end you have to update value of that counter. Add also some logic to reset it, the way you need it. But point is that you have to store it somewhere outside of your code...since after call is executed all values are lost.




回答2:


Have you tried using LIMIT with two arguments. The first will be your starting point and the second your offset. eg:

SELECT * FROM tbl LIMIT 5,10;  # Retrieve rows 6-15

To adapt it to the example:

$sql = 'SELECT * FROM t1
     RIGHT OUTER JOIN t2 ON t2.wid = t1.wid
     LIMIT ' . $number . ', 678';

Maybe see MySQL: Select for more help.



来源:https://stackoverflow.com/questions/29363469/i-want-these-lines-to-run-for-all-of-the-values-of-my-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!