1. question: I have never understood how to write an SQL statement to calculate a specific result. code: {code ...} the SQL statement I wrote is like this, but the result is not what I want. I want to remove duplicates and store comment as an array to make it only have one result...
1. Question: I have never understood how to write an SQL statement to calculate a specific result.
2. Code:
SELECT tb.*,tbc.`comment` from blog_tinyblog AS tb,blog_tbcomment AS tbc where tb.user_id=tbc.user_id AND tb.num=tbc.now_numgroup by tbc.num_tiny_blog
The SQL statement I wrote is as follows:
But this is not the result I want. I want to remove duplicates and store comment as an array, so that there is only one result. Please be barefoot. Distinct may be used, but not
Purpose: I want to write a Weibo comment system (now I want to display comments corresponding to each microblog]
Reply content:
1. Question: I have never understood how to write an SQL statement to calculate a specific result.
2. Code:
SELECT tb.*,tbc.`comment` from blog_tinyblog AS tb,blog_tbcomment AS tbc where tb.user_id=tbc.user_id AND tb.num=tbc.now_numgroup by tbc.num_tiny_blog
The SQL statement I wrote is as follows:
But this is not the result I want. I want to remove duplicates and store comment as an array, so that there is only one result. Please be barefoot. Distinct may be used, but not
Purpose: I want to write a Weibo comment system (now I want to display comments corresponding to each microblog]
Let me attack you:
-
There are some problems with your database design. We recommend that you read mysql-related books when you are doing something. (Use num for association ?)
-
Group by usage
-
SQL statement query can be performed in uppercase to improve the efficiency, but you should not use a mix of uppercase and lowercase keywords.
-
Select tb. *, tbc. comment
From blog_tinyblog as tb, blog_tbcomment as tbc
Where tb. num = tbc. num_tiny_blog
-
If all of the above can be tolerated, I can only talk about things in the format of your questions.Messy layout and logicAnd you still use images. Can we try it?
Most ORM generally has two ways to solve this problem:
-
UsedLazy LoadMethod, in fact, the principle is very simple: Take twice, take the blog for the first time, take the comment corresponding to the blog for the second time, but the execution of the second operation is delayed.
-
The second method is the query method of the subject, but the ORM maps the query result to the desired form (a single Blog object contains the corresponding Comment list ).
To sum up, if the subject is using an ORM tool, it generally includes the two methods for processing. If no ORM is used, the subject needs to implement a corresponding algorithm (in fact, it is similar to the reduce algorithm ).
-
I don't understand what you mean.
-
What do you want to do.
-
First, you do not need to query all links.
-
Furthermore, there is no aggregate function. It makes no sense to use grouping.
The best solution is to get only the number of Weibo posts and comments. The specific comments can be obtained using ajax, so there is no need to retrieve them once. This is a great pressure.
If you have to obtain the group_concat function at a time, you can check the mysql manual. However, this function is not recommended. If you comment a lot, the efficiency may be very low or even cause errors.
In addition, your table design is unreasonable. For example, the number of Weibo posts, the number of comments, and the number of comments without comments are useless, when users Delete Weibo posts or comments, you need to maintain the corresponding data on multiple records. It is enough to have a total_comments field in the Weibo table. As for the number of records displayed, the serial number must be (page number-1) * Number of records displayed on each page + number of records on the current page, so there is no need to put it in the database.