Skip to content
Advertisement

How to get next/previous double records in MySQL?

hello everyone I ask for help to understand how to proceed with the sql code I have a table set as in the photo

enter image description here

in the kiss_count cell do duplicates in the records, see photos. how can I retrieve records for example from 6 onwards? I tried to do something like this, but it doesn’t respect the list, skip kiss_count 6 and go directly to 2, the problem is that there are duplicates like 6 6 or 11, how can I proceed? I could lean on the id list to respect the calls in the right irdine, the code that yso is this:

WHERE kiss_count < '6'  ORDER BY kiss_count_total DESC LIMIT 5");

the question is can I order a list with duplicates?

I have also tried this

WHERE kiss_count < '6' AND ID < '8'  ORDER BY kiss_count_total DESC LIMIT 5");

BUT IT DOES NOT WORK

Advertisement

Answer

So you want to order the kiss_count field which has duplicates. To do this you need to use an extra field. Reading the title, you also want to be able to get the next 5 values.

You can use the order by on 2 columns, no need for the where clause:

select * from my_table order by kiss_count desc, id asc;

Because the query select * from my_table order by kiss_count desc, id asc always gives you the same sequence, you can grap results using offset.

So in your example, the second page should have an offset of 5, and a row_count of 5:

// page 2
select * from my_table order by kiss_count desc, id asc limit 5, 5;
// page 3
select * from my_table order by kiss_count desc, id asc limit 10, 5;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement