2011-08-31 119 views
0

我有一個數據庫:database與表:table和一些領域:MySQL和PHP,如何比較數字

id fname  dphone  count_pic dup_id 

6055903 Karla  5126xxx798 1  57 
6173767 Aaliyah  4082xxx534 4  39 
5611411 Aaliyah  4082xxx534 15  39 
5611211 Aaliyah  4082xxx534 18  39 
4234798 Abby  3057xxx974 31  16 
6166691 Walter  6178xxx280 1  74 
3375576 Walter  6178xxx280 17  74 

什麼,我試圖做的是選擇具有最小count_pic的領域和那些有更大的count_pic和有相同的dup_id

任何想法如何在MySQL中做到這一點?

謝謝。

+0

怎麼辦你想要結果出來嗎?你想每個'dup_id'有一行嗎?或者是其他東西? –

回答

5

有了這個查詢,你會選擇每dup_id

SELECT MIN(count_pic) AS minpic, 
     MAX(count_pic) AS maxpic, 
     dup_id 
    FROM `table` 
GROUP BY dup_id 

count_pic最小和最大的價值。如果你還需要相應的行,那麼你可以使用類似

SELECT * 
     FROM `table` t1 
INNER JOIN (SELECT MIN(count_pic) AS minpic, 
        MAX(count_pic) AS maxpic, 
        dup_id 
       FROM `table` 
      GROUP BY dup_id) t2 ON t1.dup_id = t2.dup_id 
          AND (t1.count_pic = minpic 
           OR t1.count_pic = maxpic) 
+0

嘿,如果我還想添加'WHERE t1.fname = t2.fname'。我一直試圖做一段時間沒有結果。我會很感激。謝謝 – Patrioticcow