Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Web client requests records by batches of 10 specifying the from parameter. How do I need to write SQL query to select records from 1 to 10, from 11 to 20 and so on?
LIMIT is what you are looking for
SELECT
*
FROM
`table`
ORDER BY
`somecolumn`
LIMIT 0, 10
LIMIT 0, 10 means starting from record 0 take 10 rows. LIMIT 10, 10 would be the next page skipping 10 and taking 10 more rows.
Use the offset of the limit clause and don't forget to order by a specific column to get always the same results. To get records 21 to 30:
select * from your_table
order by some_column
limit 20, 10
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have a column name called last_activity that is of type DATETIME. (so. 2021-01-03 00:00:00). I want to find all rows which are within 5 minutes of now. I attempted the following:
SELECT *
FROM users
WHERE last_activity > DATE_SUB(NOW(),INTERVAL 5 MINUTE)
I get 0 rows, even though the activity is within 5 minutes (I know because the database says so). How can I fix this?
Use the function TIMESTAMPDIFF() to get the difference in minutes:
SELECT *
FROM users
WHERE ABS(TIMESTAMPDIFF(MINUTE, last_activity, NOW())) <= 5
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i want to select info which was registered in database earlier first one by one,
its i want to select oldest info first then newer, like to be in order (first come first serve) one by one
like
Today - some info
Yesterday - some info
day before yesterday - some info
SELECT info FROM table ORDER BY A DESC LIMIT 1
table
id info date_registered
1 john 9999-12-31 23:59:59
Your question is vague, but there's various ways you can do it.
What are you doing with this?
check out OFFSET and LIMIT, which you'd do something like run your query and then increase the offset each time you run it.
just use whatever programming language you're using to loop over the result set? IE remove the limit.
3 it multiple times and add a where clause which uses the previous return value.
SELECT info
FROM table
where date_registered < '9999-12-31'
order by date_registered desc
limit 1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to run a query in sequelize or SQL where select current month data and only show just upcoming date data like some kind of events if that day pass then next event or date will be showing like this I have 3 dates in this month
1 2020-03-15
2 2020-03-22
3 2020-03-27
So before 15 I want to only no 1 date after 15march pass I want only no 2 date and goes on
If you want to show the next date in the future, then it would be something like:
select t.*
from t
where date > current_date
order by date asc
fetch first 1 row only;
The exact syntax might vary by database -- say now() or getdate() instead of current_date; or select top (1) or limit 1 instead of the fetch clause.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
SELECT * FROM tablename WHERE
(Border <= 10 AND Border2 >= 100) OR (Border <= 200 AND Border2 >= 500)
But, i'd like to take values of left and right border from query
I try to explain
make multi-condition for query in query
SELECT TOP 1 Border, Border2 FROM table2 WHERE id in (1,2,3,4,5)
If you want those values, then don't select all columns with *, instead select only the columns you want:
SELECT leftBorder, rightBorder FROM...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to show the month with the highest number of quantity sold.
I don't know how to do that. Please help thank you.
I have:
id, prod_name, price, q_sold, month
the q_sold must sum up with the number of month input.
You can order your result by the sum and take only the first entry with limit 1
select `month`, sum(q_sold) as sold_sum
from your_table
group by `month`
order by sold_sum desc
limit 1