2013-03-19 129 views
-1

這是查詢的輸出: enter image description hereSQL查詢GROUP BY ID過濾器?

 
SELECT * FROM 
        ((SELECT 
         privatemsgs.id, 
         privatemsgs.useradn, 
         privatemsgs.useraid, 
         privatemsgs.title, 
         privatemsgs.created, 
         privatemsgs.timee, 
         privatemsgs.isread, 
         u.photo AS creatorphoto, 
         privatemsgs.relatedto 
        FROM privatemsgs 
        LEFT JOIN 
         users AS u ON(privatemsgs.useraid = u.id) 
        WHERE userbid='$myid' 
         AND relatedto=0 and bdel=1) 
        UNION 
        (SELECT 
         privatemsgs.id, 
         privatemsgs.useradn, 
         privatemsgs.useraid, 
         privatemsgs.title, 
         privatemsgs.created, 
         privatemsgs.timee, 
         privatemsgs.isread, 
         u.photo AS creatorphoto, 
         rel.relatedto 
        FROM privatemsgs AS rel 
         JOIN privatemsgs ON(rel.relatedto = privatemsgs.id) 
         LEFT JOIN 
         users AS u ON(rel.useraid = u.id) 
        WHERE rel.userbid='$myid' 
        )) privatemsgs 
        GROUP BY id 
        ORDER BY timee DESC 

我有雙號 「2」。第一個是「isread = 0」,第二個是「isread = 1」。

當我補充說:「通過ID組」,我有(2號線)

但我需要輸出到顯示isread = 1(如3號線)

如何解決呢?

+0

請提供查詢 – 2013-03-19 13:12:49

+1

提示:'MAX(isread)'和'GROUP BY id' – 2013-03-19 13:12:50

+0

發佈查詢本身plz:D我們不知道您使用的是什麼查詢:) – imclickingmaniac 2013-03-19 13:12:59

回答

0

在*附近添加MAX(isread)。 GROUP BY僅適用於聚合函數

+0

作品:)很棒! – GuyChabra 2013-03-19 13:21:48