数据库结构来存储用户对布局顺序的偏好

Database structure to store the User 's preference of layout order

网页布局中有几个选项

  1. 最新消息
  2. 推荐新闻
  3. 已关注新闻
  4. 历史新闻
  5. 观看次数最多的新闻

用户可以select布局的顺序,例如他们可以将观看次数最多的新闻排在最前面。

所以我在考虑如何将选择存储在table中,方便开发

选择数量固定,只有这5个选择 用户会经常更新订单

我在想:

创建 user_choice table

user_id
latest (nullable , integer)
recommend (nullable , integer)
follow (nullable , integer)
history (nullable , integer)
most_view (nullable , integer)

所以,当用户注册时在 table 中创建记录,并且每当更新更改行时,这种方法似乎是可行的,但也不是直接重新排序程序中的布局

那么,有没有更好的结构思路呢?

感谢您的帮助

我将使用 user idlayout_idorder_id 创建一个 table layout_order,这种方式很容易添加更多布局,而无需添加更多列你的 table.

创建新用户时,您会分配一个默认订单。

  user_id     layout_id   order_id
    1            1           1
    1            2           2
    1            3           3
    1            4           4
    1            5           5

这里是UPDATE的例子。该布局需要 @user_id@layout_id@order_id

这里我用变量创建了一个新的rank,有一个特殊的ORDER BY

SqlFiddle Demo你可以查一下return只是JOIN

里面的SELECT
SET @layout_id = 5;
SET @order_id = 2;
SET @user_id = 1;

UPDATE layout_order L 
JOIN (SELECT  l.*, @rownumber := @rownumber + 1 AS rank
      FROM layout_order l 
      CROSS JOIN (select @rownumber := 0) r
      WHERE user_id = @user_id
      ORDER BY CASE
                    WHEN layout_id =  @layout_id THEN @order_id -- here is the variable
                    WHEN order_id  <  @order_id THEN order_id   -- order doesn't change
                    WHEN order_id  >= @order_id THEN order_id + 1                    
               END

     ) t
  ON L.user_id = t.user_id
 AND L.layout_id = t.layout_id
SET L.order_id = t.rank;

我会朝这个方向前进:

create table user
(   -- your pre-existing user table, this is a stub
    id int auto_increment primary key
    -- the rest
);

create table sortChoices
(   -- codes and descriptions of them
    code int auto_increment primary key,
    description varchar(100) not null
);

create table user_sortChoices_Junction
(   -- intersect or junction table for user / sortChoices
    userId int not null,
    code int not null,
    theOrder int not null,
    primary key (userId,code),  -- prevents dupes
    constraint `fk_2user` foreign key (userId) references user(id),
    constraint `fk_2sc` foreign key (code) references sortChoices(code)
);

结点table驱动它,灵活,后面的人不会把自己锁在同一个想法里'there will only ever be 5'

此外,对于其他更喜欢 CSV 值的人来说,还有数据规范化问题。这是我为此所做的 write-up,并与连接表相关联。

所以,这对于后面的人来说和 OP 问题一样多。