Determine if the 'Like' button was pressed in the post list

Asked 2 years ago, Updated 2 years ago, 72 views

There is a bulletin board table and a like table separately

Bbs.java

private int id;
private String title;
private String content;
private int likeCount;
private User writeUser;
// I don't know if I should put it in
private Set<Likes> likes;
Likes.java

private int id;
private int userId;
private int boardId;

If you press like, press like count +1 on the bulletin board tablewith an increase of

If you already said yes, it's good In the table, the user likes the post I'm trying to block you by judging if you've ever done it

When I recall the list, I wonder what to do with judging whether or not it is something I liked.

Is it possible if you put a list of people who like it in the form of Set on the bulletin board table?

I'm worried that SQL statements will become too complicated and cause performance problems when printing the list

You like me I wonder if there is any other way to judge a sentence than that.

spring java

2022-09-22 21:53

3 Answers

Import Posts

SELECT * FROM Bbs WHERE <Condition>

If the purpose of the above query is to "determine whether or not I liked it,"

SELECT Bbs.*, CASE WHEN Likes.id IS NULL THEN false ELSE true END FROM Bbs LEFT JOIN Likes ON Bbs.id=Likes.boardId AND Likes.userId=<myId> WHERE <Condition>

I think we can ask questions like above.

The precondition should be that only 1 like per article (BBS) is connected by LEFT JOIN. To do this, one user should be able to do only one like per sentence. Therefore, in the Likes table, userId and boardId must be UNIQUE KEY. In LEFT JOIN, you can write Likes.userId=<myId> in the ON statement so that one person can specify one like per text (BBS).

This causes the record in Bbs to JOIN with one Likes record or all columns corresponding to Likes to JOIN with NULL by ON Bbs.id=Likes.boardId AND Likes.userId=<myId>

If you do so, you can determine if you have ever been Liked with the syntax CASE WHEN Likes.id IS NULL THEN false ELSE true END. You can think of this as an if phrase that makes Likes.id true if it exists or false if it does not. Some DBs provide this syntax as a function. You can use functions such as ifnull for mysql, nvl for Oracle, and isnull for MS-SQL.


2022-09-22 21:53

Assuming you put the list in JSON

{'Who ID', 'Hong Gil Dong ID', 'Doggy ID'}...

As it continues to pile up, management becomes complicated.

It's very convenient if you get rid of it and do it as follows.

SELECT 0 FROM Likes WHERE userId = 'My id' AND boardId = 'glid'

Assuming that your writing ID is 5, if you liked it,

It's good if you follow the query above You get my user ID and my current writing ID, 5, from the record table.

If you don't, I won't bring anything, and if you do, I'll bring anything, so you can check with it

We just need to check if it's there anyway, so we can reduce the db load by setting it to SELECT 0


2022-09-22 21:53

When I read the bulletin board list, I think I should read all the Likes that correspond to the list of boardId and check if they are in this list within the application. I don't think I need a set of bulletin board tables.

SELECT * FROM Likes WHERE userId = 1 AND (boardId > 1 AND boardId < 20)

If you use postgresql, you can use the Array type column, which is convenient to read the list by adding the userId array column you like to the bulletin board table.

http://www.postgresql.org/docs/9.4/static/arrays.html


2022-09-22 21:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.