2011-02-24 22 views
1

的開始execuse我的英語,但我需要一個SQL幫助:MySQL的:獲取具有唯一的列行每

我有提供了示例表:ID,id_user,報價,時間戳

我需要得到與排序,這將是頁面上的一個id_user一個行(例如每10行只會有一個獨特的id_user和具有時間戳二階)


例子:

1, Pepa, Auto, 1.1.2011 
2, Pepa, Motorka, 1.1.2011 
3, Karel, Traktor, 2.1.2011 
4, Lukas, Jeep, 2.1.2011 
5, Pepa, Autokara, 3.1.2011 
6, Jindra, Traktor, 5.1.2011 

=>排序(2排頁)

**1. Page** 
1, Pepa, Auto, 1.1.2011 
3, Karel, Traktor, 2.1.2011 


**2. Page** 
2, Pepa, Motorka, 1.1.2011 
4, Lukas, Jeep, 2.1.2011 


**3. Page** 
5, Pepa, Autokara, 3.1.2011 
6, Jindra, Traktor, 5.1.2011 

感謝您的幫助,簡單的 「一個頁面上的唯一用戶提供了」!

+0

你可以寫一個僞代碼算法,解釋你如何決定哪些行應該去哪裏?在計算上很難找到一種排列數據的方式,以便沒有用戶在同一頁面上出現兩次。這就像揹包問題,這是NP-Complete。 – mellamokb 2011-02-24 15:30:48

+0

您是否需要處理沒有多行的情況?例如,在您的示例中,如果您想在頁面上顯示5行,它將如何工作?仍然有3頁,因爲有三個Pepa行,即使總共只有6個條目? – jswolf19 2011-06-06 03:55:58

回答

0

您是否嘗試過「group by」?

delimiter // 

connect pepa 

drop table if exists offers; 
create table offers (
id SERIAL, 
id_user VARCHAR(20) NOT NULL, 
offer VARCHAR(20) NOT NULL, 
timestamp TIMESTAMP DEFAULT NOW() 
); 

insert into offers 
(id_user,offer) 
values 
('Pepa', 'Auto'), 
('Pepa', 'Motorka'), 
('Karel', 'Traktor'), 
('Lukas', 'Jeep'), 
('Pepa', 'Autokara'), 
('Jindra', 'Traktor'); 

select * from offers group by id_user order by timestamp; 

// 

這產生了:

id id_user offer timestamp 
4 Lukas Jeep 2011-06-05 21:14:10 
6 Jindra Traktor 2011-06-05 21:14:10 
1 Pepa Auto 2011-06-05 21:14:10 
3 Karel Traktor 2011-06-05 21:14:10 

注意,沒有id_users的重複。如果您包含條件語句(「where」),則可以基於登錄人員的id_user創建唯一頁面。

希望這有助於您。 :)

乾杯!

(請注意,通過時間戳排序可能是有點怪在這裏。不要忘了,你可以隨時與您選擇[PHP等]的語言進行後期處理。)

+0

此查詢具有GROUP BY但只返回唯一行,但我需要具有特定ORDER的所有行(每個頁面只有唯一行)。 – frosty22 2011-06-06 07:13:57

0

這個PHP代碼將產生您在問題中列出的相同輸出。它可能不是世界上最有效率的東西,但它完成了工作。

你也許可以寫一個時髦的MySQL查詢來做到這一點,但我不知道它會如何擴展成千上萬的記錄等。而你正在製作網頁。 :)

<?php 

// Collect stuff from the database 
$dbc=mysqli_connect('127.0.0.1','user','passwd','pepa') or 
die('Could not connect!'); 
$getOffers='select * from offers'; 
$rs=mysqli_query($dbc,$getOffers); 
while($thisRow=mysqli_fetch_assoc($rs)) 
$offers[]=$thisRow; 
mysqli_close($dbc); 

// Create the pages 
// (this is probably a bit over the top, but you get the idea) 
foreach($offers as $oI => $thisOffer) 
$offers[$oI]['used']=false; // <-- tell us if we've used the record or not 
$thisUser='Pepa'; // <-- the user who should appear at the top of each page 
$numRecsPerPage=2; // <-- the number of records per page 
$cPg=-1; foreach($offers as $oI => $thisOffer) { 
    if($thisOffer['id_user']==$thisUser) { 
     $cPg++; 
     $offers[$oI]['used']=true; 
     $page[$cPg][]=$thisOffer; 
     $recsUsed=1; foreach($offers as $pI => $procOffer) { 
      if(!$offers[$pI]['used'] && $offers[$pI]['id_user']!=$thisUser) { 
       $offers[$pI]['used']=true; 
       $page[$cPg][]=$procOffer; 
       $recsUsed++; 
      } 
      if ($recsUsed>=$numRecsPerPage) break; 
     } 
    } 
} 

// Print the pages 
foreach($page as $thisPage) { 
    foreach($thisPage as $thisRow) 
    echo $thisRow['id']."\t".$thisRow['id_user']."\t". 
      $thisRow['offer']."\t".$thisRow['timestamp']."\n"; 
    echo "\n"; 
} 

?> 

輸出:

1 Pepa Auto 2011-06-05 21:14:10 
3 Karel Traktor 2011-06-05 21:14:10 

2 Pepa Motorka 2011-06-05 21:14:10 
4 Lukas Jeep 2011-06-05 21:14:10 

5 Pepa Autokara 2011-06-05 21:14:10 
6 Jindra Traktor 2011-06-05 21:14:10 

對不起添加另一個答案 - 我本來添加評論,但我覺得代碼更有助於在這裏。