duanpanhuo0618 2012-10-14 07:29
浏览 32

PHP MySQL,按更新字段分组

I am currently working on a project, and I have been searching around for a while about the specific problem I am having, so I came here to post the question.

Basically, I have a table which handles the notifications on the site I am developing, and I am using GROUP BY to handle redundant notifications, like if someone commented on the same thing multiple times, I only want to show the latest notification. Here is an example of query I am using (I am using the PDO driver in PHP):

SELECT * FROM
((SELECT `a`,`b`,`c` FROM `table` WHERE `c` IN ($array) AND `b` IN ('a','b') AND  `read`='false' AND `id`!='xxx' GROUP BY `c` )
UNION ALL
(SELECT `a`,`b`,`b` FROM `table` WHERE `b` IN ('a','b') AND `id`='xxx' AND `read`='false' ) ) as x ORDER BY `id` DESC LIMIT 15

The problem lies in after this query, so basically, in my Notifications window, when the user views all of these, I set all of their read columns to true. Now when the user reloads the page, it will show the next notification that I was purposefully intending to hide in the first place.

Basically, how can I set everything that doesn't match the original query to true, so it doesn't show again after the original notification was viewed. Now if a new notification is created after the fact, I still want these to be shown, obviously.

If you have any suggestions, please let me know.

  • 写回答

1条回答 默认 最新

  • dongzhun1857 2012-10-16 00:17
    关注

    I figured this out, by the way, just in case anyone ever runs into this problem in the future. I stored each of the IDs in an array while I was looping through the result set. After this I just set up another query to set all of the ID's that were not IN the array set to "read" between the two original lowest and highest ID's. It works perfectly. Not sure I communicated the answer the best here, but if anyone has any questions about it, let me know.

    Thanks again!

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!