Why offset. limit returns all rows? - mysql

The following SQL query return all rowsa instead from 12 to 18.
SELECT `reviews`.*,
`users`.`username` as `user_username`,
`users`.`slug` as `user_slug`
FROM `reviews`
JOIN `users` ON `users`.`id` = `reviews`.`user_id`
WHERE `reviews`.`product_id` = 2
ORDER BY `reviews`.`created_at` DESC LIMIT 12, 18
As result I have to get 6 rows instead all.

Using the OFF SET in the LIMIT query
The OFF SET value is also most often used together with the LIMIT keyword. The OFF SET value allows us to specify which row to start from retrieving data
Let’s suppose that we want to get a limited number of members starting from the middle of the rows, we can use the LIMIT keyword together with the offset value to achieve that. The script shown below gets data starting the second row and limits the results to 2.
SELECT * FROM `members` LIMIT 1, 2;
Refrence and MySQL docs
In your case to return only 6 rows starting from 12(thirteen row) it should be ORDER BY reviews.created_at DESC LIMIT 12, 6

Related

Select last two values from two IDs

I would like to select two specific values, the first value is the last inserted row where the ID_SENSOR is 1, and the second value is the last inserted row where the ID_SENSOR is 2.
My Database table:
My Query:
SELECT DATA FROM (SELECT * FROM registovalores WHERE ID_SENSOR = '1' OR ID_SENSOR = '2' ORDER BY ID_SENSOR DESC LIMIT 2) as r ORDER BY TIMESTAMP
My Query is printing the last value just from the ID_SENSOR 1, which it means that I'm only getting the last inserted values, and not the last inserted value from both IDS.
I would like to print my values like this:
ID_SENSOR 1 = 90
ID SENSOR 2 = 800
What do I need to change on my Query?
Thank you.
One method uses a correlated subquery:
SELECT rv.*
FROM registovalores rv
WHERE rv.ID_SENSOR IN (1, 2) AND
rv.TIMESTAMP = (SELECT MAX(rv2.TIMESTAMP)
FROM registovalores rv2
WHERE rv.ID_SENSOR = rv2.ID_SENSOR
);
You have to have two separate queries, one per sensor.
select id_sensor, data
from the_table
where id_sensor = 'sensor_1'
order by timestamp desc -- the latest value is the first to come
limit 1; -- only pick the top (latest) row.
If you want to query for more than one value in a single database roundtrip, consider using union all between several such queries.
Please note that such a query may return one row or zero rows, since data for a particular sensor may not be available yet.

Data's order is not correct in Mysql

Here is my DB data's sample:
both two columns are int
tuanId ,tuanSort
'375579', '55'
'370576', '54'
'366222', '54'
...
'346268', '52'
'369608', '52'
'370587', '52'
'370775', '52'
...
'370225', '52'
'370588', '52'
'360758', '52'
'366390', '51'
and I try these sqls bellow:
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 140,20;
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 160,20;
and I get these wrong data, I want to make a pagination, but the second page has some same data in the first page:
For Example, the first pic's 17th row has shown twice in the 2 pics
So, is the sort value the same can cause such a problem? Or MySQL has problem with such a select?
Given that tuanSort is not unique, the behavior is within the specification.
You are observing that one query returns a particular row as 157th row. In another query execution, it's returned as the 161st row.
To get more deterministic sequence, specify additional columns in the ORDER BY clause, e.g.
ORDER BY tuanSort DESC, tuanId DESC
If the intent behind this sequence of statements is "paging", there are more efficient approaches, such as saving a unique, sequenced identifier from the "last" row of the previous page.
If tuanSort,tuanId tuple is unique...
WHERE tuanSort <= :last_tuanSort
AND ( tuanSort < :last_tuanSort OR tuanId < :last_tuanId )
AND ...
ORDER BY tuanSort DESC, tuanId DESC
LIMIT 20
If you fetch all twenty rows, save tuanSort and tuanId from that last row. On the next "page", supply those saved values in the query predicates.
But that's an answer to a question you didn't ask.
I think you need to refresh your database table.
Try this : Go ->TuanItem->Click Operations->Alter table order by-> select your tuanid and set as an ascending order.
I think your problem is, your getting same data in 2 query.
then change the limit 160,20 to
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 140,0;
SELECT * FROM `tuan`.`TuanItem` WHERE ... ORDER BY `tuanSort` DESC LIMIT 160,141;
the 0 is the offset which means the data start at 0 and the second query start at the 141 so that you cannot get same value twice

mysql: SELECT WHERE id IN() and others

I'm using this query to select a set of records from a MySQL database:
SELECT *
FROM table
WHERE id IN(10,14,12,11,8,7,4)
AND actief='on'
ORDER BY FIELD(id,10,14,12,11,8,7,4)
This will give me the given ID's. In this case I will get a maximum of 7 records. But it can be less if for example ID '14' has active='off'.
But what I need is a set of 20 records where het list IN(10,14,12,11,8,7,4) must be in the result if they also meet the condition active='on'. If this returns 6 records, then I want the query to select another 14 records. Selecting highest ID first, must meet active='on' and may not already be in the result.
Can this be achieved by one SQL statement. Or should I first put the result of the mentionend query in an array and in a second array select the remaining records. And finaly put those also in the array?
You want to sort rather than filter the results. I think this is the query you want:
SELECT *
FROM table
ORDER BY (id IN(10,14,12,11,8,7,4) AND actief = 'on') desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
EDIT:
The final solution only wanted actief = 'on', so:
SELECT *
FROM table
WHERE actief = 'on'
ORDER BY (id IN (10,14,12,11,8,7,4)) desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;

limiting number of elements in a result set

I'm trying to limit the number of elements in my result set by setting a limit to my sql logic. I have two seperate functions to achieve what I want. The first one has a limit I've set manually e.g 0, X. The second function has two extra arguments that is min and max and these are set as the limit. But when the min and max are e.g 7, 14 it gives me more elements then 7. There are no duplications in the result set since I have unique id's on each element and they check out. Also the integers passed to the sql function have the correct intervall.
What am I doing wrong?
"SELECT table1.*, table2.user_id FROM table1 LEFT JOIN table2 ON table1.col1 = table2.col2
WHERE table1.col1 = :param1 AND table1.col2 = 1 AND table1.col3 = 0 ORDER BY table1.col4 DESC LIMIT $min, $max";
The syntax is SELECT Syntax is not
limit min, max
but
limit offset, row_count
so, limit 7, 14 says retrieve 14 rows at offset 7.

MySQL choose a random row from two joined tables

I know there are simmilar questions out there, but here`s my implementation on a fast random select row:
SELECT i.id, i.thumb_img, i.af, i.width, i.height
FROM images_detail id
JOIN images AS i ON id.imageid = i.id
WHERE id.imageid >=1
AND id.newsroom =1
AND i.width > i.height
AND id.imageid >= FLOOR( 1 + RAND( ) *23111593 )
LIMIT 1
The problem with this query is that indifferent of the RANDOM expression in id.imageid >= FLOOR( 1 + RAND( ) *23111593 ) it always returns the same ID, why?
Any help please?
Later edit:
The query takes 0.0005 seconds, using EXPLAIN, it reports back USING WHERE and 12993 ROWS returned
The Id's are auto-incremented, it's not 23111593 because RAND() returns 0.xxxxx so RAND()*23111593, returns about 12993 rows. The problem is, the same ID is at the top and I don`t want to call an ORDER BY clause.
http://www.greggdev.com/web/articles.php?id=6
or use
ORDER BY RAND() LIMIT 0,1;
Do you have any "imageid" in your database that is greater than the span you define (1<=X<=23111593)?
You might have only one entry in the DB.
(also, keep in mind that you might only have one that actually fits the query of id.newsroom = 1, i.width>i.height etc.)
Eventually I've loaded ALL the results in an array, stored it in a cache and made in PHP $key=rand(0,sizeof(array)) and then used it as follows array[$key]['property']