armoni
V2EX  ›  编程

如何处理网站中如帖子评论数,帖子点击数,是否已收藏,是否已关注这类数据

  •  
  •   armoni · Dec 16, 2012 · 4300 views
    This topic created in 4894 days ago, the information mentioned may be changed or developed.
    RT,目前数据有如评论数就在数据库设计时和table帖子放在一起,用的时候一起读,可是是否已经收藏这类跟用户个人相关的问题操作起来很不方便,需要执行多条sql来生成页面,大家是这么处理的呢。目前小站没有也暂时不考虑nosql。
    1 replies    1970-01-01 08:00:00 +08:00
    keakon
        1
    keakon  
       Dec 16, 2012   ❤️ 1
    需要一个子查询:

    table Post:
    id
    name
    ...

    table Favorite:
    id
    user_id
    post_id
    ...

    SELECT post_id, user_id FROM
    (SELECT post.id AS post_id, favorite.user_id AS user_id
    FROM post LEFT OUTER JOIN favorite on post.id = favorite.post_id) AS post_with_favorite
    WHERE user_id = ... OR user_id IS NULL;
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5629 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 01:25 · PVG 09:25 · LAX 18:25 · JFK 21:25
    ♥ Do have faith in what you're doing.