使用PDO設置LIMIT時忽略數據庫重復

[英]Ignore the database duplicates when setting LIMIT with PDO


I want to ignore the duplicates in my database when I will set my "LIMIT 0, 50", then "LIMIT 50, 50" then LIMIT..... I will need to scan the duplicates on only 1 column of my table, not all the columns at once. I can't merge the duplicates because they are different in a way : these duplicates have different prices.

我想忽略數據庫中的重復項,當我設置“LIMIT 0,50”,然后“LIMIT 50,50”然后LIMIT .....我將需要在我的表的一列上掃描重復項,不是所有的列一次。我無法合並重復項,因為它們在某種程度上是不同的:這些重復項具有不同的價格。

more precisely, I will need to show a list of these items, but to show their different prices at their right.

更確切地說,我需要顯示這些項目的清單,但是要顯示它們的右邊不同的價格。

I need a precise number (50) per pages, so I cant load less then go to the next page. I could therefore load more from the beginning (changing the max and previous offsets if i'm on a far page) in a way that if i ignore the duplicates, I will got exactly 50 per pages and I will get the good number of pages shown at the end.

我需要一個精確的數字(50)每頁,所以我不能加載少,然后轉到下一頁。因此,我可以從頭開始加載更多(如果我在遠頁上更改最大和以前的偏移量),如果我忽略重復項,我將獲得每頁50個,我將獲得良好的頁數最后顯示。

I'm a bit beginner with PHP and I have no idea about how to do that. Maybe pre-scan all the table and then start writing my code, by being flexible with my scan's variables of LIMIT and everything ? what functions I need ? how ?

我是PHP的初學者,我不知道如何做到這一點。也許預先掃描所有表格,然后開始編寫我的代碼,靈活使用我的掃描變量LIMIT和一切?我需要什么功能?怎么樣 ?

Else, do something pre-programmed or a function of php that I don't know it exists can solve this problem ? Or I really need to get an headhache xD

否則,做一些預先編程的東西或者我不知道它存在的php函數可以解決這個問題嗎?或者我真的需要得到一個頭腦xD

3 个解决方案

#1


1  

I am not entirely certain of what you are asking, but I think you might want to do a aggregate statement along these lines:

我不完全確定你在問什么,但我想你可能想要按照這些方式做一個匯總的陳述:

select
    itemID,
    group_concat(itemPrice)
from
    yourTable
group by
    itemID
limit 50

This will bring back a list of 50 items and a second column where all the prices are grouped together. Then in your PHP code, you can either explode() that second column keep it as is.

這將帶回50個項目的列表和第二列,其中所有價格組合在一起。然后在您的PHP代碼中,您可以爆炸()第二列保持原樣。

Edit: If you select every field, you can't then use an aggregate function. If you want to select other columns that won't be different, add them to both the select and the group by sections like this:

編輯:如果選擇每個字段,則無法使用聚合函數。如果要選擇其他不同的列,請按以下部分將它們添加到select和group:

select
    itemID,
    itemName,
    itemSomething,
    itemSomethingElse,
    group_concat(itemPrice)
from
    yourTable
group by
    itemID,
    itemName,
    itemSomething,
    itemSomethingElse
limit 50

#2


1  

Probably you can group by item, and use GROUP_CONCAT to show different prices list? In this way you can still use LIMIT 50. If the price column is numeric, cast it to VCHAR.

您可以按項目分組,並使用GROUP_CONCAT顯示不同的價格表?通過這種方式,您仍然可以使用LIMIT 50.如果price列是數字,則將其強制轉換為VCHAR。

#3


0  

I admit I borrowed the group_concat() function from the other answers :)

我承認我從其他答案中借用了group_concat()函數:)

After reading this paragraph from the docs:

從文檔中閱讀本段后:

The default behavior for UNION is that duplicate rows are removed from the result.
The optional DISTINCT keyword has no effect other than the default because it also
specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal
does not occur and the result includes all matching rows from all the SELECT statements.

Assume the following table (testdb.test):

假設下表(testdb.test):

ID  Name    Price
1   Item-A  10
2   Item-A  15
3   Item-A  9.5
4   Item-B  5
5   Item-B  4
6   Item-B  4.5
7   Item-C  50
8   Item-C  55
9   Item-C  40

You can page this table rows (9 rows) or groups (3 groups, based on the item's name).

您可以分頁此表行(9行)或組(3組,基於項目的名稱)。

If you would like to page your items based on the item groups, this should help:

如果您想根據項目組分頁,這應該有所幫助:

SELECT 
    name, group_concat(price)
FROM
    testdb.test
GROUP BY name
LIMIT 1 , 3 
UNION SELECT 
    name, group_concat(price)
FROM
    testdb.test
GROUP BY name
LIMIT 0 , 3; -- Constant 0, range is the same as the first limit's

If you would like to page your items based on all the items (I don't think that's what you were asking for, but just in case it helps someone else), this should help:

如果你想根據所有項目分頁你的東西(我不認為這是你要求的,但以防萬一它可以幫助其他人),這應該有所幫助:

SELECT 
    name, price
FROM
    testdb.test
LIMIT 1 , 5 
UNION SELECT 
    name, price
FROM
    testdb.test
LIMIT 0 , 5; -- Constant 0, range is the same as the first limit's

A very important thing to note is how you'll have to modify the limits. The first limit is your key, you can start from any limit you'd like as long as it's <= count(*) but you will have to have the same range as the second limit (i.e 3 in the first example and 5 in the second example). And the second limit will always start from 0 as shown.

需要注意的一件非常重要的事情是如何修改限制。第一個限制是你的密鑰,你可以從你想要的任何限制開始,只要它是<= count(*)但你必須具有與第二個限制相同的范圍(即第一個例子中的3個和5個第二個例子)。如圖所示,第二個限制將始終從0開始。

I enjoyed working on this, hope this helps.

我喜歡這方面的工作,希望這會有所幫助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/07/28/7252a78f09299c1efd087231c60d6ad6.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com