I am trying to create a SQL call that first order all the rows by date and returns all the rows starting from the number 25.
So far I have
"SELECT * FROM 'users' ORDER BY 'table'.'regdate' OFFSET 24"
But this is not working .. How can I do this ?
try like this:
first get count all data in table like this:
$count=select count(*) from users
then
SELECT * FROM 'users' ORDER BY 'table'.'regdate' limit 25,$count
There is nothing wrong with your query, the problem must be elsewhere.
Your query includes an offset which omits the first 24 results. This imposes no limits and should therefore return evrey row pat the 24 oldest dates. To omit the new dates instead you have to set your order as DESC
SELECT * FROM 'users' ORDER BY 'table'.'regdate' OFFSET 24
To do that and limit results to 100 records simply do this:
SELECT * FROM my_table ORDER BY date_field DESC OFFSET 24 LIMIT 100
Related
I have a query that fetches a set of records as shown:
select id
from wallactions
where status=1 and created > DATE_ADD(NOW(),INTERVAL -1 DAY)
order by id desc LIMIT $priorRecordsCnt
The LIMIT variable $priorRecordsCnt changes often and can grow very large. It's necessary because i need to know the last id in that set of records based on the $priorRecordsCnt count value.
The problem is, I need to only access the last id in this set, as shown:
$last=array_values(array_slice($result,-1))[0]['id'];
I feel like this is pretty expensive to just get the id of the last record in the set.
Is there a way to optimize this query so it uses the count variable $priorRecordsCnt but i don't need to fetch all the records just to obtain the last value?
The limit clause takes two arguments - an optional offset and the row count. Instead of using $priorRecordsCnt as the record count, you should use it as the offset, and limit the record count to 1:
select id
from wallactions
where status=1 and created > DATE_ADD(NOW(),INTERVAL -1 DAY)
order by id desc LIMIT $priorRecordsCnt, 1
-- Here ---------------------------------^
I have data in MYSQL table where i have to paginate through 4 rows at a time.
To do this I use the command below where i increase X from 0 by 4 until i reach the end of the data.
The command works for X=0 and X=4, when i reach X=8 i get the error #1038 - out of sort memory, i tried increasing it to 256K but same result.
Anybody know how to solve? Im using PHP
SELECT DISTINCT * FROM ((SELECT DISTINCT * FROM table WHERE (scope=0) AND (id='6')) UNION (SELECT DISTINCT * FROM table WHERE (scope=0) AND (id<=1000))) as total ORDER BY timestamp DESC LIMIT X,4
Not an answer, but your query seems to be functionally identical to this:
SELECT columns
, you
, actually
, want
FROM table
WHERE scope = 0
AND id<=1000
ORDER
BY timestamp DESC
LIMIT X,4
So, I'm writing an application to fetch records from the database, and my client has asked if I could sort the data by area. I've got the following query:
SELECT
*
FROM
customers
WHERE
account_type != 'x'
AND
account_type != 'tor'
ORDER BY
area ASC,
id ASC
LIMIT 30
The query returns 30 rows worth of data, all with an area value of 1.
I've got an AJAX query set up to fetch another 30 rows, however as I am ordering the data by area, I am unable to just fit a simple range string to my query as shown in the example below:
...
AND
id > 30
...
How would I write a query that would collect the next 30 records from the database?
Also, there is a record with an area value of 0. This is not shown as the first record in the list. Why is this? And how can I fix this?
Thanks
First, combine the where conditions into a single not in:
SELECT c.*
FROM customers c
WHERE account_type NOT IN ('x', 'tor')
ORDER BY area ASC, id ASC
LIMIT 30;
Then, for the next set, use:
SELECT . . .
OFFSET 30
LIMIT 30
This assumes that id is unique in the table. That is important, because that makes the sort stable.
Have a look at the the SELECT query documentation:
https://dev.mysql.com/doc/refman/5.7/en/select.html
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
In summary, just limit returns you x records. With the offset you can set a starting position, basically allows you for paging.
MySQL LIMIT take an optional parameter offset, so you could send in a parameter telling where to start when picking the 30 results you want
SELECT
*
FROM
customers
WHERE
account_type != 'x'
AND
account_type != 'tor'
ORDER BY
area ASC,
id ASC
LIMIT 30, 30
Should give you the next 30 rows starting from row 30
The table have 11 rows
Query
SELECT COUNT(*) FROM `zars_all` ORDER BY id DESC LIMIT 9,9
Result
Zero Rows, even there are two rows having id 10 and 11
Query
SELECT * FROM `zars_all` ORDER BY id DESC LIMIT 9,9
Result
Two Rows having id 10 and 11
I have tried using column names as value in Count but it does't help. Any help is appreciated and please tell me if I am doing anything wrong here.
Your first sentence SELECT COUNT(*) returns an integer.
That means that the trailing LIMIT 9,9 is setting a minimum of 9 elements and a maximum of 9 elements.
Let’s examine the LIMIT offset, count clause parameters:
The offset specifies the offset of the first row to return.
The count specifies the maximum number of rows to return.
Your offset is out of bounds, that is why you get no rows.
Please remove that and leave it like:
SELECT COUNT(*) FROM `zars_all` ORDER BY id DESC
Waqas,
LIMIT cannot be applied directly along with COUNT instead change your query like
SELECT count(*) FROM (select * from zars_all limit 9,9) as a"**;
I've been searching all over Google and cant seem to come up with the right answer yet ( I'm just probably not searching the correct terms ), I'm trying to get 25 results returned from a database each time but for example what I want to do is:
query 1 should return results 1 - 24
query 2 should return results 25 - 49
and so on.
What would be the best way to do this?
Thanks
They are separate queries althought they return the exact same fields from the exact same tables under the exact same conditions. The difference between them is the LIMIT chunk:
LIMIT startRow, rowsCount
--the first row is the 0th, not 1st.
If your query is like:
select * from mytable where field1 = "value1"
you must execute two separate queries (or as much queries as you want):
select * from mytable where field1 = "value1" LIMIT 0, 25
-- will return the first 25 rows
select * from mytable where field1 = "value1" LIMIT 25, 25
-- will return 25 rows starting from the row 25
general formula: If you want to fetch a specific page N (starting from 1), where each page has M elements, you must append to your query:
$yourquery = "select * from mytable where myfield = 'myvalue'";
$yourquery .= sprintf(" LIMIT %d, %d", ($page-1)*$itemsPerPage, $itemsPerPage);
$result = mysql_query($yourquery, $yourconnection);
Disclaimers:
mysql_ is deprecated. use mysqli_ instead.
never use, in real production code, sprintf for your parameters unless you know what are you doing, since you could expose a SQL Injection hole.
Edit You can use ANY elements you want to do LIMIT. Usually, pages have constant size, but since you want irregular sizes (page 1 24 elements, page 2 25 elements, you should) use the following LIMITs:
LIMIT 0, 24
--gets the first 24 (say 1st to 24th) elements
LIMIT 24, 25
--gets the following 25 items (say 25th to 49th)
From the manual:
The LIMIT clause can be used to constrain the number of rows returned
by the SELECT statement. LIMIT takes one or two numeric arguments,
which must both be nonnegative integer constants (except when using
prepared statements).
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
So in your case:
SELECT * FROM `table_name` LIMIT 0,25
SELECT * FROM `table_name` LIMIT 25,25
(SELECT *
FROM mytable
LIMIT 0,24)
UNION
(SELECT *
FROM
mytable
LIMIT 24,25)
ORDER BY someSortOfId;
Note that there should be at least one column with unique values (such as an ID), or the result sets will overlap.
SELECT * FROM `table_name` LIMIT 0,25 // This will give you 1-24 records
SELECT * FROM `table_name` LIMIT 25,25 // This will give you 25-49 records