Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to limit by duplicates in a column in mysql?

After performing a query, I have a table like this:

Date Mode Count
2022-12-20 A 1
2022-12-20 B 11
2022-12-20 C 8
2022-12-21 A 10
2022-12-21 C 4
2022-12-22 A 100
2022-12-22 B 48
2022-12-22 C 81

It shows, for each day, the count of each of the different modes. If a certain mode is 0, it’s not in the table.

I’m using dynamic queries to get the data, and I want to LIMIT the quantity that it’s returned.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example, if I limited it to two days, I would want to get only this:

Date Mode Count
2022-12-20 A 1
2022-12-20 B 11
2022-12-20 C 8
2022-12-21 A 10
2022-12-21 C 4

i.e., all the rows with the first two days.

How can I do it?

EDIT I have a REST API developed using Spring Boot / JPA. I’m sending a parameterised query through the server – and I only have the initial date and the number of days onwards I need to filter. I now see that I can find a way to simply send the query using a BETWEEN operator, by doing some calculations before.

>Solution :

Using the between operator:

SELECT * FROM TABLE WHERE DATE BETWEEN '2022-12-20' AND '2021-12-22'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading