[英]how do I equally divide entries in a database into 5?
我有一個名為Shops的表,我想將此Shop條目划分為5。我的表中有80k條目,我希望用戶A可以訪問16k的第一個商店,用戶B可以訪問下一個16k的商店,等等,等等。我知道我可以使用限制,但是Shop的主鍵/ ID不是連續的。
目前,我的查詢如下所示:
if (in_array("ADMIN1", $userRoles)) {
$lowerLimit = 0;
$upperLimit = $numberOfProspectiveShops/5;
} else if (in_array("ADMIN2", $userRoles)) {
$lowerLimit = $numberOfProspectiveShops/5;
$upperLimit = ($numberOfProspectiveShops/5) * 2;
} else if (in_array("ADMIN3", $userRoles)) {
$lowerLimit = ($numberOfProspectiveShops/5) * 2;
$upperLimit = ($numberOfProspectiveShops/5) * 3;
} else if (in_array("ADMIN4", $userRoles)) {
$lowerLimit = ($numberOfProspectiveShops/5) * 3;
$upperLimit = ($numberOfProspectiveShops/5) * 4;
} else if (in_array("ADMIN5", $userRoles)) {
$lowerLimit = ($numberOfProspectiveShops/5) * 4;
$upperLimit = $numberOfProspectiveShops;
}
$prospectiveShopsQuery = $em->createQueryBuilder()->select('s')
->from("AppMainBundle:ProspectiveShop", 's')
->andWhere('s.id <= :upperLimit')
->setParameter('upperLimit', $upperLimit)
->andWhere('s.id > :lowerLimit')
->setParameter('lowerLimit', $lowerLimit)
->andWhere('s.status IS NULL')
;
您應該使用mySQL的“限制”功能,而不要使用基於id的where子句。
這可以通過使用setFirstResult($ offset)和setMaxResults($ limit)來實現。 對於前5000個,偏移量應等於0,限制應等於5000,對於接下來的5000,偏移量應等於5000,限制值為5000
您的查詢生成器應如下所示:
$prospectiveShopsQuery = $em->createQueryBuilder()->select('s')
->from("AppMainBundle:ProspectiveShop", 's')
->andWhere('s.status IS NULL')
->setFirstResult($offset)
->setMaxResults($limit)
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.