简体   繁体   English

MySql限制和偏移给出了错误的结果

[英]MySql limit and offset giving wrong result

I'm getting only 99 records for limit 100 after 9900 offset . 在9900偏移后,我只获得了99条限制100的记录 Even though i have 2,00,000 records in db with left join in both the tables with distinct. 即使我在db中有2,00,000条记录,但左边的连接在两个表中都是不同的。 What's wrong with my query or loop 我的查询或循环有什么问题

Even i tried the query in phpmyadmin there also it was giving same result 99 records. 即使我在phpmyadmin尝试了查询,它也给出了相同的结果99条记录。

Query 询问

select distinct(table1.id), table2.name, table2.uuid from table1
        left join table2 on table1.id = table2.id limit 9900, 100

Laravel Query: Laravel查询:

$this
->database
->table('table1')
->selectRaw('distinct(table1.id), table2.uuid, table2.name')
->leftJoin('table1.id', '=', 'table2.id')
->where('opponent_uID', '>', $uID)
->skip($offset)
->take($limit)
->get();

Loop

$limit = 100;
$offset = 0;
while (true) {
     $result = $this->query($limit, $offset);
     $offset += $limit;
     if (empty($result)) {
         break;
     }
     // Logic here
}

This may not be right answer to address why it was giving the 99 records. 这可能不是正确答案,以解决它为什么给出99条记录。 But i when i play with that query i found these solutions 但是,当我使用该查询时,我发现了这些解决方案

Issue: 问题:

  1. One main issue with query is i don't have order by. 查询的一个主要问题是我没有订单。

MySQL MyISAM tables make no guarantees on result with out order by on limit, offsets MySQL MyISAM表不对结果进行保证,按限制,偏移量排序

Solutions: 解决方案:

  1. Using order by in query Giving 100 results but not tested with entire loop 在查询中使用order by给出100个结果但未使用整个循环进行测试

    select distinct(table1.id), table2.name, table2.uuid from table1 left join table2 on table1.id = table2.id order by table1.id limit 9900, 100 select table(table1.id),table2.name,table2.uuid from table1 left join table2 on table1.id = table2.id order by table1.id limit 9900,100

  2. Strangely if i use table2.* instead of some fields in same query with out order by gives 100 records 奇怪的是,如果我使用table2。*而不是相同查询中的某些字段,而不是按顺序给出100条记录

    select distinct(table1.id), table2.* from table1 left join table2 on table1.id = table2.id order by table1.id limit 9900, 100 select table(table1.id),table2。* from table1 left join table2 on table1.id = table2.id order by table1.id limit 9900,100

So, Finally it's only my assumption for the problem is there might chance of null values in left join tables give wrong result. 所以,最后我唯一的问题就是左边连接表中的空值可能会产生错误的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM