当前位置:网站首页>[solved] data duplication or data loss after laravel paginate() paging
[solved] data duplication or data loss after laravel paginate() paging
2022-06-26 12:18:00 【Special sword】
【 resolved 】Laravel paginate() After paging Duplicate data or Data loss problem
Problem scenario description :
- It is known that there is a student table and a consumption points table . Now the student table is arranged in positive order according to the consumption points of each student ( That is, the integral is arranged from high to low ).
- Student list students And Consumption points table orders The table is a one to many relationship ( A piece of student data Corresponding Multiple voting points data ).
Examples of data tables
Student list (students)
id | name( Student name ) |
---|---|
1 | Balsam pear |
2 | Washbasin King |
3 | Old stick |
Consumption points table (orders)
id | vote( vote ) | students_id( Student list id) | is_pay( Whether to pay ) |
---|---|---|---|
1 | 2 | 1 | 1 |
2 | 3 | 3 | 1 |
3 | 4 | 1 | 0 |
4 | 5 | 2 | 1 |
5 | 2 | 1 | 1 |
6 | 1 | 2 | 1 |
7 | 3 | 2 | 1 |
8 | 2 | 1 | 1 |
9 | 6 | 3 | 1 |
Error code example
At this point, the acquired data is used by the front end laravel Own paging $data->links(); Data loss will occur Or data duplication
public function studentList(Request $request){
// Get a list of students
$data = Student::select('students.*',DB::raw('(SELECT SUM(contribution_value) FROM orders WHERE orders.students_id = students.id and orders.is_pay = 1) as sum_contribution_value'))
->orderBy('sum_contribution_value','desc')
->paginate(10);
return view('h5.student.studentList',compact('data'));
}
Example of rendered error data :
Solution :
Examples of correct code :
public function studentList(Request $request){
// Get a list of students
$data = Student::select('students.*',DB::raw('(SELECT SUM(contribution_value) FROM orders WHERE orders.students_id = students.id and orders.is_pay = 1) as sum_contribution_value'))
->orderBy('sum_contribution_value','desc')
>orderBy('students.id','desc')// Add this line of code .order by Because sorting is not unique , So the order of each query is different , The data segment intercepted by paging may have duplicate data or cause data loss
->paginate(10);
return view('h5.student.studentList',compact('data'));
}
Return the reason why the data is lost or duplicated
As shown in the error code example above :->orderBy('sum_contribution_value','desc')
Only this identifier is used for sorting , However, this representation is not a unique identifier for the entire returned data !
Mysql Use order by When sorting, the sorting ID is not unique , So the order of each query is different , The data segment intercepted by paging may have duplicate data or cause data loss .
Therefore, it is sufficient to add a unique identification field for sorting ( For example, the uniqueness of a table id Or fields with unique constraints such as order number ).
边栏推荐
- One click deployment CEPH script
- Hello! Forward proxy!
- Scala-day03- operators and loop control
- Oracle lock table query and unlocking method
- Basic principle of MOS tube and important knowledge points of single chip microcomputer
- 证券账户可以开通 开户安全吗
- 11、 Box styles and user interface
- Ctfshow web getting started command execution web75-77
- 房租是由什么决定的
- 【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
猜你喜欢
Pratique de l'attaque et de la défense du réseau HUST | 6 Expérience de sécurité du microprogramme de l'équipement IOT | expérience 2 technologie d'atténuation des attaques de l'équipement IOT basée s
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Scala-day03- operators and loop control
Ctfshow web getting started command execution web75-77
Scala problem solving the problem of slow SBT Download
4. N queen problem
Random numbers in leetcode 710 blacklist [random numbers] the leetcode path of heroding
11、 Box styles and user interface
How to prevent weight loss under Gao Bingfa?
Scala-day06- pattern matching - Generic
随机推荐
国际美妆业巨头押注中国
UDP协议详解[通俗易懂]
Oracle锁表查询和解锁方法
18: Chapter 3: development of pass service: 1: SMS login & registration process, introduction; (SMS verification code is used here)
Five problems and solutions of member operation
One click deployment CEPH script
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Five trends of member marketing of consumer goods enterprises in the future
Assembly language (7) operation instruction
International beauty industry giants bet on China
webgame开发中的文件解密
Five trends of member management in 2022
Using the methods in the repository to solve practical problems
HUST网络攻防实践|6_物联网设备固件安全实验|实验三 FreeRTOS-MPU 保护绕过
The loss of female scientists
Statistical genetics: Chapter 2, the concept of statistical analysis
Excel operation of manual moving average method and exponential smoothing method for time series prediction
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
Mqtt disconnect and reconnect
2016年四川省TI杯电子设计竞赛B题