V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
armoni
V2EX  ›  编程

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

  •  
  •   armoni · Dec 16, 2012 · 4280 views
    This topic created in 4887 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   ·   2891 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 07:20 · PVG 15:20 · LAX 00:20 · JFK 03:20
    ♥ Do have faith in what you're doing.