简体   繁体   English

带有限制的 SQL 查询在 phpmyadmin 中不起作用

[英]SQL Query with Limit doesn't work in phpmyadmin

I put this sql code in PHPMYADMIN to display the first 4 rows , but it display all the 43 rows of my table .我将此 sql 代码放在 PHPMYADMIN 中以显示前 4 行但它显示了我的表的所有 43 行 I have no idea why.我不知道为什么。 Please help !请帮忙 !

Here is the query :这是查询:

  SELECT  * FROM `myTable` 
    natural JOIN `column1` 
    natural JOIN `column2` 
  WHERE `mytable`.`something` = 1 
  ORDER BY mytable_date DESC LIMIT 0,4

Many thanks in advance提前谢谢了

I think this may be because phpMyAdmin modifies SELECT statements, adding it's own LIMIT clause: It may be replacing your LIMIT with its own, for some reason.我认为这可能是因为 phpMyAdmin 修改了 SELECT 语句,添加了它自己的 LIMIT 子句:出于某种原因,它可能会用自己的 LIMIT 替换你的 LIMIT。

phpMyAdmin inserts a LIMIT because by default it paginates the result of a SELECT when you view it in the GUI, it doesn't show all rows right away for large tables (there's a checkbox you can click to do that, once the initial first page of results is displayed). phpMyAdmin 插入一个 LIMIT 是因为默认情况下,当您在 GUI 中查看它时,它会对 SELECT 的结果进行分页,它不会立即显示大表的所有行(您可以单击一个复选框来执行此操作,一旦初始第一页结果显示)。

It's not always really clever about it though: I've just run into a related difficulty where it was inserting a LIMIT clause into a subquery with FOR UPDATE, which MariaDB doesn't allow... and it was giving me an error.不过,它并不总是很聪明:我刚刚遇到了一个相关的困难,它将 LIMIT 子句插入到带有 FOR UPDATE 的子查询中,这是 MariaDB 不允许的……它给了我一个错误。

So you have to remember when using phpMyAdmin, that it adds LIMIT clauses to your SQL, it isn't sent to the database as-is.因此,您在使用 phpMyAdmin 时必须记住,它将 LIMIT 子句添加到您的 SQL,它不会按原样发送到数据库。

I think there might be an option somewhere to turn this behavior off, but OTOH I don't know where that is.我认为可能有一个选项可以关闭这种行为,但 OTOH 我不知道那在哪里。 (If someone knows, please point it out in the comments!) (如果有人知道,请在评论中指出!)

You can use LIMIT and OFFSET for this.您可以为此使用 LIMIT 和 OFFSET。 In your case you just need LIMIT, because you're starting since 0.在您的情况下,您只需要 LIMIT,因为您从 0 开始。

SELECT  * FROM `myTable` 
natural JOIN `column1` 
natural JOIN `column2` 
WHERE `mytable`.`something` = 1 
ORDER BY mytable_date DESC LIMIT 4 OFFSET 0;

or simpler或更简单的

 ORDER BY mytable_date DESC LIMIT 4;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM