简体   繁体   中英

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 . 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 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).

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.

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.

I think there might be an option somewhere to turn this behavior off, but OTOH I don't know where that is. (If someone knows, please point it out in the comments!)

You can use LIMIT and OFFSET for this. In your case you just need LIMIT, because you're starting since 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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