我一直试图让我的代码完全正确地工作,它在很大程度上完美地工作,但有一个例外,它很难用语言解释,所以我会试着告诉你我在做什么用。
首先,到目前为止,这是我的MYSQL QUERY,(所有这些都需要保持不变,或至少完成同样的事情)
$notificationsq = mysql_query("SELECT N.*, P.*,
(CASE
WHEN
(N.action = 2 AND N.state = 0) THEN 1
ELSE N.state
END) AS state
FROM
notifications N,
posts P
WHERE N.userID='$session' AND
(N.action='1' OR N.action='2') AND
N.uniqueID=P.id AND
P.state='0'
GROUP BY N.uniqueID
ORDER BY N.state ASC, N.date DESC
");
这是我的通知表设置(与帖子表相关的代码与我的问题无关) http://i43.tinypic.com/2wd6phl.png
每行代表什么(只有不自解释的行):
现在“状态”仅返回未读的帖子(action = 1)(状态= 0)但是如果任何帖子获得新评论,我希望它将“状态”返回0作为帖子。< / p>
就像, IF(action == 2 AND state == 0 WHERE uniqueID == uniqueID){state = 0}
答案 0 :(得分:3)
怎么样:
SELECT
`N`.*,
`P`.*,
`N`.`state` AS `state`
FROM
(
SELECT
*
FROM
`notifications`
WHERE
`userID`='$session'
AND
(
(
`action`='1'
AND
`state` = 0
)
OR
`action`='2'
)
ORDER BY
`state` ASC,
`date` DESC
) AS `N`,
`posts` AS `P`
WHERE
`N`.`uniqueID`=`P`.`id`
GROUP BY
`N`.`uniqueID`