当前位置:网站首页>[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 ).
边栏推荐
- webgame开发中的文件解密
- Re recognized! Know that Chuangyu has been selected as one of the first member units of the "business security promotion plan"
- Investment forecast and development strategy analysis report of China's rural sewage treatment industry in 2022
- This executeQuery (SQL) cannot compile classes for JSP. What is the reason?
- 2、 MySQL Foundation
- 【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
- VMware virtual machine bridging mode can not access the campus network "suggestions collection"
- NFS共享存储服务安装
- Is it safe to open an account in the top ten securities app rankings in China
- Flannel's host GW and calico
猜你喜欢
![[probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test](/img/2f/f44381ea759f4c1c957a8f9434f0ee.png)
[probability theory] conditional probability, Bayesian formula, correlation coefficient, central limit theorem, parameter estimation, hypothesis test

Vscode solves the problem of Chinese garbled code

Black squares in word

Ctrip ticket app KMM cross end kV repository mmkv kotlin | open source

SQL injection in Pikachu shooting range

HUST network attack and defense practice | 6_ IOT device firmware security experiment | Experiment 3 freertos-mpu protection bypass

Build Pikachu shooting range and introduction
![Random numbers in leetcode 710 blacklist [random numbers] the leetcode path of heroding](/img/58/2a56c5c9165295c830082f8b05dd98.png)
Random numbers in leetcode 710 blacklist [random numbers] the leetcode path of heroding
女性科学家的流失

Cet article présente la moyenne mobile quadratique linéaire et le fonctionnement simple d'Excel pour réaliser la prédiction des séries chronologiques dans la modélisation.
随机推荐
HUST网络攻防实践|6_物联网设备固件安全实验|实验二 基于 MPU 的物联网设备攻击缓解技术
fastjson的JSONArray和JSONObject[通俗易懂]
2021 q3-q4 investigation report on the use status of kotlin multiplatform
Consumer goods enterprises, four pain points of member marketing
Measures to support the development of cultural and creative industries in Futian District, Shenzhen
国际美妆业巨头押注中国
Using the methods in the repository to solve practical problems
China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
Spark-day01- get started quickly
Loggie encoding and newline character test
VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
Scala problem solving the problem of slow SBT Download
Redux related usage
Deep thinking from senior member managers
I want to know whether flush is a stock market? Is online account opening safe?
Analysis report on China's photovoltaic inverter market prospect forecast and investment strategy recommendations in 2022
Omnichannel membership - tmall membership 2: frequently asked questions
Prospering customs through science and technology, Ronglian and Tianjin Customs jointly build a genomic database and analysis platform
Five problems and solutions of member operation
Cross platform members get through the two channels of brand Ren Du