Что смещается запрос?

Я удивлен, что никто не упомянул Python termcolor модуль . Использование довольно просто:

from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

Или в Python 3:

print(colored('hello', 'red'), colored('world', 'green'))

Это не может быть достаточно сложно, однако, для игрового программирования и "цветных блоков", которые Вы хотите сделать...

9
задан 29 July 2009 в 01:12
поделиться

2 ответа

Соответствует оператору LIMIT :

SELECT something FROM table LIMIT $limit OFFSET $offset;

//or alternatively
SELECT something FROM table LIMIT $offset,$limit;

Другими словами , выберите что-нибудь из таблицы, но дайте мне только $ limit записи, начиная с записи $ offset .

$ offset = ($ page_no - 1) * $ limit
$ page_no основан на 1.

Дополнительная информация в документации MySQL:

12.2.8 Синтаксис SELECT

ОТКАЗ: $ limit и $ offset используются в этом вопросе только для облегчения понимания. Конечно, вы не хотите строить запрос без надлежащего экранирования значения.

14
ответ дан 4 December 2019 в 10:33
поделиться

That particular comment unfortunately confuses two common ways to consider pagination or grouping in queries. The SELECT syntax, as Andrew describes, allows an OFFSET parameter, the number of items to skip before returning anything. However, it is most often used with pagination, as in the pagination library from which your quotation was taken. In this case, it is more useful to ask for a specific page number.

To compare the two, consider a case where you've made a search and have gone to page 3, with 10 items per page. Items 1-20 were on the first two pages; therefore, the OFFSET parameter will be 20:

SELECT * FROM table WHERE searched LIMIT 10 OFFSET 20

or

SELECT * FROM table WHERE searched LIMIT 20,10

Unfortunately, the $page_no parameter in the cited example probably wants to be 3, the page number. In that case, the SQL offset will have to be calculated. Given that get_items does not seem to be a standard model method, it's probably calculated in there; alternatively, the pagination library seems to have a property called sql_offset which probably calculates it for you. Either way, the calculation is easy enough:

$offset = max($page_no - 1, 0) * $limit;
6
ответ дан 4 December 2019 в 10:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: