The correct syntax for SQL.

Source: Internet
Author: User

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:

    1. 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?) )

    2. Use of GROUP by

    3. The capitalization of the SQL statement query increases the efficiency of the wire, but you do not need to use the keyword case mix

    4. Select Tb.*,tbc.comment
      From Blog_tinyblog as tb,blog_tbcomment as TBC
      where Tb.num=tbc.num_tiny_blog

    5. 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:

    1. 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.

    2. 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).

    1. I don't understand what you mean.

    2. What you want to do.

    3. First of all, this full-link query is generally not necessary.

    4. 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.