我有一個簡單的用戶記錄表。允許一個電子郵件地址有多個條目。我現在要創建一個將返回所有記錄的查詢,但將重複條目的數量限制爲一個設置的數字,例如每個電子郵件地址5個。選擇每組的最大行數
這個查詢的最終結果我將用來創建一個新表。
我唯一的想法是使用PHP並選擇一個唯一的電子郵件地址列表,然後我將使用這個列表循環查詢表並將其限制爲5,但我的表中有大約65k獨特的電子郵件總記錄約284k行。
有沒有辦法讓我在MySQL中做到這一點?
這是表結構。
first_name, last_name, email
John, smith, [email protected]
john, smith, [email protected]
John, smith, [email protected]
john, smith, [email protected]
Jane, smith, [email protected]
jane, smith, [email protected]
Jane, smith, [email protected]
john, smith, [email protected]
我想獲得一個查詢,該查詢返回相同的記錄,但將重複項限制爲可以更改的參數。在上面的例子中,假設允許最多2個重複項。我想要得到
first_name, last_name, email
John, smith, [email protected]
john, smith, [email protected]
Jane, smith, [email protected]
jane, smith, [email protected]
希望這個更清楚一點。
謝謝。
可以告訴你一個簡單的數據,你從它需要的是什麼? –
使用衆多解決方案之一獲取每個組的行號,然後只選擇行<= 5的行。 – Barmar
您的表中是否有ID列? – peterm