-1

好的,这就是我想要的

数据库

id | fromm | ontvanger | date | msg | gelezen
--------------------------------------------------
1  | kees  | marc     | time_stamp  | 0
1  | hans  | marc     | time_stamp  | 1
3  | kees  | marc     | time_stamp  | 0
5  | selma | marc     | time_stamp  | 1
6  | kees  | marc     | time_stamp  | 0
7  | kees  | marc     | time_stamp  | 0

这是我想要的 GROUP BY fromm

1  | hans  | marc     | time_stamp  | 1
3  | kees  | marc     | time_stamp  | 0
5  | selma | marc     | time_stamp  | 1

$result = mysql_query("SELECT * FROM berichten where ontvanger = '$session->username' 
GROUP BY fromm ORDER BY date DESC
");

显示最新记录,gelezen 为 0 第一

4

1 回答 1

1
SELECT  a.*
FROM    berichten  a
        INNER JOIN
        (
            SELECT  fromm, MAX(date) date
            FROM    berichten
            GROUP   BY fromm
        ) b ON  a.fromm = b.fromm AND
                a.date = b.date
WHERE   a.ontvanger = '$session->username'
ORDER   BY (gelezen = 0) DESC, id

子查询可让您获取 date每个fromm. 然后将其结果连接回原始表以获取列的其他值。

(gelezen = 0)产生一个布尔值,如果它为真,1否则将给出0。由于您要基于 对行进行gelezen = 0排序,因此对布尔运算的结果应用降序。

于 2013-09-22T19:04:52.200 回答