Mysql – Only specific variations of LIMIT are working for me

MySQL

I spent most of yesterday coding a "TOP 10" page for my website to pull from my database.

It was a pretty simple SQL statement:

SELECT * FROM `cards` GROUP By `name` ORDER BY `viewsperweek` DESC LIMIT 10;

It was all working perfectly for me last night and the info was correct but when I woke up this morning it no longer works.

After some testing it seems only LIMITS of 1-5 will work. If I put it past 5 now the SQL result now longer shows on the website (it DOES continue to work on the database itself).

So basically now I can seemingly only do this:

SELECT * FROM `cards` GROUP By `name` ORDER BY `viewsperweek` DESC LIMIT 5;

Can't for the life of me figure out why it would work last night and not this morning.
The code itself is being called from a js file which in turn calls the php file for the SQL code.

Best Answer

Fixed it finally.

Was an issue with the I was using GROUP BY, ORDER BY and LIMIT, at least I think.

I changed it to the following and it's working smoothly now:

SELECT DISTINCT `name`, `id`, `viewsperweek`, `totalviews` FROM `cards` GROUP BY `name` ORDER BY `viewsperweek` DESC LIMIT 10

I ran into this issue before with my Database and forgot about it. There are some cards that are duplicates so I needed to remove them through the SQL code. In my original code it was hitting some duplicates after the 5th and breaking. Honestly not sure why (as the code worked in my back-end db), but it's working now.