为查看的通知组成mysql查询

时间:2015-05-14 14:37:15

标签: php mysql sql

我正在撰写通知提醒功能。我有一张表Notification,其中有一个字段ViewedViewed的值为TINYINT。当用户查看通知时,它会翻转到1。然后基于Viewed我显示准备好警报的通知。现在,如果没有新的通知,我想显示最近查看的通知。我使用的查询是

$query="select * from notification where NotifierId=? and Viewed=0 order by
NotificationDate DESC ";

现在我想要做的是伪语言

Select Fields from notification where NotifierId=someid and Viewed=0 Incase 
there are new notifications but retreive the old as well Viewed=1 if the new
notifications are less than 10 ordered by date

如果有任何办法,请告诉我。感谢

1 个答案:

答案 0 :(得分:0)

我想你可能会对这样的事情感兴趣:

select
    *
from
    notification
where
    NotifierId=? and
    Viewed in (0, IF((select count(*) from notification where Viewed = 0)<10,1,0))
order by
    Viewed asc,
    NotificationDate desc

这将导致所有未查看的通知按日期降序排序,然后查看的通知将按日期降序显示。

我希望我能够完全理解你的问题,但如果没有,请告诉我。