1. Question: I have never known a SQL to figure out a specific result, and want to ask the great God
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 I wrote is like this, and the result
But this is not the result I want, I want to remove the repetition, save comment as an array, become only one result form, please the big God barefoot. Maybe it might be distinct, but no.
Purpose: I would like to write a microblog review system "Now I want to show the corresponding comments for each microblog"
Reply content:
1. Question: I have never known a SQL to figure out a specific result, and want to ask the great God
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 I wrote is like this, and the result
But this is not the result I want, I want to remove the repetition, save comment as an array, become only one result form, please the big God barefoot. Maybe it might be distinct, but no.
Purpose: I would like to write a microblog review system "Now I want to show the corresponding comments for each microblog"
I'll blow you off:
There are some problems with your database design. It is recommended that you look at MySQL-related books when you are doing something. (associated with Num?) )
Use of GROUP by
The capitalization of the SQL statement query increases the efficiency of the wire, but you do not need to use the keyword case mix
Select Tb.*,tbc.comment
From Blog_tinyblog as tb,blog_tbcomment as TBC
where Tb.num=tbc.num_tiny_blog
If all these things can be tolerated, then I can only take the format you ask questions typesetting chaos, logic chaos and you still use the picture, let us knock up to try?
Most ORM generally has two ways of solving this type of problem:
Adopt the Lazy Load way, in fact, the principle is very simple: Take two times, the first time to take the blog, the second time to take the blog corresponding to the comment, but the second time to take the operation delayed execution.
The second is the way the main question is queried, but the ORM maps the results of the query to the form you want (a single Blog object contains the corresponding comment list).
So, in general, if the master is using an ORM tool, it usually contains the two methods of processing, if not using any ORM, the main need to implement a corresponding algorithm (in fact, a similar algorithm like reduce).
I don't understand what you mean.
What you want to do.
First of all, this full-link query is generally not necessary.
Furthermore, there is no point in grouping without aggregation functions.
The best solution is to just get the microblog and the number of comments on the line, the specific comments can be used Ajax to fetch, no need to take out at once, so the pressure is larger.
If you have to take it out at once you can consider group_concat this function, specifically to check the MySQL manual, but not recommended, if the comments a lot of the efficiency will be very low or even create errors.
In addition, your table design is not reasonable. such as the number of micro-blogs, the first few comments, no comments these three kinds of comments represent the field is useless, users delete Weibo or delete comments when you want to maintain the data on multiple records, the trouble is dead. It is sufficient to have a total_comments field on the Weibo table. As for the end is the first few, show time naturally know, the serial number must be (page-1) * The number of records displayed per page + the first line of the current page, so there is no need to put in the database.