Limit not work in sql

$sql = "INSERT INTO test1 (entity_id, test, created)
SELECT entity_id, test, :c_time

FROM test2 AS t
WHERE entity_type = :c_repn
GROUP BY entity_id
LIMIT :c_start, :c_lim
ON DUPLICATE KEY UPDATE test = VALUES(test), created = VALUES(created)";

db_query($sql,$arr);

Comments

Alexandr.P created an issue. See original summary.

yash_khandelwal’s picture

Try this example.

INSERT INTO table_2
SELECT * FROM table_1
LIMIT 10
ON DUPLICATE KEY UPDATE table_2.id = table_2.id

I hope, this will help you.

Alexandr.P’s picture

in your limit no placeholder

Tart0’s picture

LIMIT tag only accept INT type, ensure your vars contains only integer then retry

$var1 = ( int) 0;
$var2 = ( int) 100;
Alexandr.P’s picture

the problem lies elsewhere LIMIT :c_start, :c_lim == LIMIT '0', '10'

I need to LIMIT 0, 10

Alexandr.P’s picture

by using db_query_range($sql, $start, $limit, $arg);

out

INSERT INTO test1 (entity_id, test, created)
SELECT entity_id, test, :c_time

FROM test2 AS t
WHERE entity_type = node
GROUP BY entity_id
ON DUPLICATE KEY UPDATE test = VALUES(test), created = VALUES(created)
LIMIT 0,10

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE

Konstantin Komelin’s picture

I think there is no need to overcomplicate things. Your query is not trivial in fact, so you may want to pass parameters directly.
Just don't forget to sanitize them.

$sql = "INSERT INTO test1 (entity_id, test, created)
SELECT entity_id, test, :c_time

FROM test2 AS t
WHERE entity_type = :c_repn
GROUP BY entity_id
LIMIT " . (int)$c_start . ", " . (int)$c_lim . 
"ON DUPLICATE KEY UPDATE test = VALUES(test), created = VALUES(created)";

db_query($sql,$arr);
Alexandr.P’s picture

Добрый день Константин. То есть Вы считаете, что принудительное изменение для лимита типа переменной, этого достаточно? Если да то тогда просто доп проверку перед вставкой в sql. и если не целое, то sql не выполняется.

Konstantin Komelin’s picture

Приветствую, Александр.

То есть Вы считаете, что принудительное изменение для лимита типа переменной, этого достаточно?

(int) - самый дешевый способ перевода из строки в число из мне известных. Кроме того, он гарантирует безопасность поступающих парметров.
В чем ваши сомнения?

Если да то тогда просто доп проверку перед вставкой в sql. и если не целое, то sql не выполняется.

Как вам удобно. Я лишь показал простейший способ того, как сделать лимит в вашем SQL запросе.

Пожалуйста, используйте английский язык, так как это правило использования drupal.org. Другие тоже должны понимать о чем идет речь.

---

Hi Alexandr,

(int) is the cheapest approach for converting a string to a number of those I know. Moreover, it guarantees safety of the parameters.
What are your doubts?

I gave a rough idea on how to implement the limit for your query. Feel free to improve the code sample.

By the way, please use English because it's one of the rules of Drupal.org. Others should be able to understand what's going on.

Alexandr.P’s picture

Да у меня хреново с англицким
Да и пусть учат международный русский язык

Version: 7.54 » 7.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

apaderno’s picture

Assigned: Alexandr.P » Unassigned
Category: Bug report » Support request
Issue tags: -MySQL