SQL查询GROUP BY id过滤器?

时间:2013-03-19 13:11:42

标签: php mysql sql database

这是查询的输出: enter image description here

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”。

当我添加“group by id”时,我有(第2行)

但我需要输出显示isread = 1(如第3行)

我该如何解决?

1 个答案:

答案 0 :(得分:0)

在*附近添加MAX(isread)。 GROUP BY仅适用于聚合函数