1

Summary: What is the most efficient way to store information similar to the like system on FB. Aka, a tally of likes is kept, the person who like it is kept etc.

It needs to be associated with a user id so as to know who liked it. The issue is, do you have a column that has a comma delimited list of the id of things that were liked, or do you have a separate column for each like (way too many columns). The info that's stored would be a boolean value (1/0) but needs to be associated with the user as well as the "page" that was liked.

My thought was this:

Column name = likes eg.:

1,2,3,4,5

Aka, the user has "like" the pages that have an id of 1, 2, 3, 4 and 5. To calculate total "likes" a tally would need to be taken and then stored in a database associated with the pages themselves (table already exists).

That seems the best way to me but is there a better option that anyone can think of?

P.S. I'm not doing FB likes but it's the easiest explanation.

EDIT: Similar idea to the plus/neg here on stackoverflow.

4

1 に答える 1

3

この場合、いいねを追跡するために新しいテーブルを作成するのが最善の方法です。したがって、すべての投稿 (ユーザーが投票できる) を含むposts列を持つ tableがあるとします。また、すべてのユーザーを含む 列post_idを持つ別のテーブルがあります。usersuser_id

likesと のような、少なくとも 2 つの列を持つテーブルを作成する必要がlike_postidありlike_useridます。これで、ユーザーが投稿にいいね! するたびに、このテーブルに、いいね! された投稿の ID ( fromの値) と投稿にいいね! したユーザーの ID ( post_idfromの値)を含む新しい行が作成されます。もちろん、テーブルにさらにいくつかの列を入力することもできます (たとえば、いいね! がいつ作成されたかを追跡するため)。postsuser_iduserslikes

ここにあるものは、多対多の関係と呼ばれます。Google で検索して、それに関する詳細情報を取得し、それらを正しく実装する方法に関するアドバイスを見つけてください (ID のコンマ区切りリストはベスト プラクティスの 1 つにならないことがわかります)。

コメントに基づく更新: 私が正しければ。アーティストに投票したすべてのユーザーのリスト (名前順) を取得したいとします。次のようにする必要があります。

SELECT Artists.Name, User.Name
FROM Artists
JOIN Votes
    ON Votes.page_ID = Artists.ID
JOIN Users
    ON Votes.Votes_Userid = Users.User_ID
WHERE Artists.Name = "dfgdfg"
ORDER BY Users.Users_Name

ここで奇妙なことがあります。アーティストIDを含むVotesテーブルの列は、と呼ばれているようpage_IDです。また、列名に少し一貫性がありません (それほど悪くはありませんが、6 か月間放置した後にコードを理解できるようにしたい場合は、覚えておく必要があります)。コメントでは、結合を 1 つだけ行うと言っていますが、実際には 2 つの結合を行います。2 つのテーブル名を指定すると (そのように: JOIN Users, VotesSQL は実際にこれら 2 つのテーブルを結合します。

コメントに投稿したクエリに基づいて、結合を使用した経験があまりないことがわかります。それらの使用方法を読むことをお勧めします。これにより、優れたコードを書く能力が本当に向上します。

于 2012-08-08T07:46:39.553 に答える