Given tables setup like this
tableA {
id = 1
name = 'bob'
id = 2
name = 'sally'
id = 3
name = 'sue'
}
tableB {
id = 1
name = 'bob'
}
If I run this command, it returns ID's 2 and 3, as wanted:
select id
from tableA a
left join tableB b using (id)
where id not in (select id from tableB)
order by id
limit 10;
But it is a very large table so I need to use offset. When I try the following command, no results are returned.
select id
from tableA a
left join tableB b using (id)
where id not in (select id from tableB)
order by id
limit 10 offset 10;
Is there a way to use offset when a join is being used?
Given tables setup like this
tableA {
id = 1
name = 'bob'
id = 2
name = 'sally'
id = 3
name = 'sue'
}
tableB {
id = 1
name = 'bob'
}
If I run this command, it returns ID's 2 and 3, as wanted:
select id
from tableA a
left join tableB b using (id)
where id not in (select id from tableB)
order by id
limit 10;
But it is a very large table so I need to use offset. When I try the following command, no results are returned.
select id
from tableA a
left join tableB b using (id)
where id not in (select id from tableB)
order by id
limit 10 offset 10;
Is there a way to use offset when a join is being used?
Share Improve this question edited 19 hours ago ValNik 6,1341 gold badge7 silver badges15 bronze badges asked 19 hours ago user3052443user3052443 8481 gold badge10 silver badges23 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 0The process environment is not visible, but I will suggest this approach. You process rows by batches (limit 10).
With parameter @startId=0
select id
from tableA a
where id>@startId
and id not in (select id from tableB)
order by id
limit 10;
After processing batch, take @startId as max(id) in processed batch and put as parameter value no next query - without offset.
select id
from tableA a
where id>@startId
and id not in (select id from tableB)
order by id
limit 10;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743764316a4503211.html
OFFSET
with aJOIN
, this is not the issue. The reason why you're getting no results after addingOFFSET 10
in the specific query you provided is because of how filtering is done after theJOIN
andWHERE
clause, especially combined withNOT IN
and the effect of your dataset size. – AztecCodes Commented 19 hours ago