English 中文(简体)
活动数据库设计和查询?
原标题:Database design and SQL query for the activity feed?

我想开展一些类似面对面的活动。 在我提出问题之前,我要解释我的我的SQL数据库设计。 我有一个用户表,即:

user_id     name     other columns
userA       -           -
userB       -           -
userC       -           -
userD       -           -

用户可以相互跟踪,这里是称为“追随者”的表格。

id      user_id         follower_id
1        userA            userB
2        userD            userB
3        userB            userA
-
-
-

As we see, in above table, userB is following userA and userD.

现在我的问题是,在用户中哪一个用户(用户B的用户)进行活动时,应当通知他。 请允许我谈谈活动,例如:

user_id     activity_type   activity_link  activity_date  
userA           note
userB           photo
userC           note
userD           photo

www.un.org/Depts/DGACM/index_french.htm

<斯特凡>问题 2 由于用户B仅跟踪用户A和用户D,因此用户B仅与他所跟踪的用户有关,如何为之建立q问? 缩略语

 Select * from activity where user_id exists in user_id in followers table where follower_id = userB 

(This is my idea, can you help me building SQL query)

由于阅读了这一长期问题和可能提供的帮助。

问题回答

是的,你将需要一个表格储存活动数据。 否则,你就必须在其他地方储存这一数据。

撰写询问的最容易的方法是加入。 在这种情况下,如果目前的用户跟随,我们只希望活动流,以便我们能够使用内力加入。

select * from activity a
    join followers f on a.user_id = f.user_id
where follower_id =  userB  

你目前的设计似乎合理,可以包括什么内容。 但是,似乎特别缺乏向用户通报new活动信息的途径。

迄今为止,我想象,它作为表格实施,请打电话到<>新_活动<>/代码>。 可能的设计类似:

follower_id int
activity_id int

或许,两者应合并为PK。

通知通过在<条码>中插入<>>>> >>>>> 触发......a. ,因此,通知可能应当成为<条码>的触发点。 但当然是这样。 在出现新的活动信息时,您应将新运行的用户与跟踪器联系起来,以形成适当的<条码> 查询/活动的配对清单,并相应列入<条码>新_active<>。

此外,我认为,还可在<条码>下限/代码>上添加一个触发点。 当出现新的<条码>用户/下限<>码>时,您可能希望通过<条码>>>>>,在最近某个时期(如一周或一天)使用该用户,能够通知新的追随者,最近发生了一些(或无)活动。

The notifications should be removed as soon as the user has been notified, i.e., I understand, as soon as the information about new activity has been requested. Now that seems a bit trickier then inserts. I think there should be a stored procedure like GetNewActivity (@follower_id int) that reads all rows from new_activity for the specified follower, returns them as a table and also removes them from new_activity so user wasn t notified about those again. And I do not see so far why all the operations could not be grouped inside a transaction. If you manage to design your system in such a way that new activity info can only be acquired via GetNewActivity kind of a way, then I think there should be no problems about repeated or missed new activity notifications.

我不想让你很乐意设计概念和实际问题。 然而,如果这证明是对你们的挑战,那么我随时准备提供帮助,而且我相信,所有社区都是如此。 我恳请后者对设计提出的所有批评,这些批评表明他们不可避免地拥有:

至于你的问题2,以及我迄今为止不肯向您提出问询的例子。 你们已经获得了。 以某种方式从用户那里获取活动,然后由特定用户(主人)进行。 我几乎肯定会给你以替代解决办法,而我则特别要这样做,但我认为这是更好的办法。

你似乎有些新意想加入或至少不相信如何使用这些编号,我谨鼓励你研究一下你提供的简单查询:<条码>/代码”,因为你需要就新活动通知提出问题。 也就是说,我要建议你首先通过 yourself努力完成这一部分工作,这就是为什么我不向您提出另一个问题的解决办法,而是向您指出上述问题:这既是一个好的解决办法,也是建立你需要的其他问题的有益起点。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签