Find the correct SQL statement.

Source: Internet
Author: User
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:

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

  2. Group by usage

  3. SQL statement query can be performed in uppercase to improve the efficiency, but you should not use a mix of uppercase and lowercase keywords.

  4. Select tb. *, tbc. comment
    From blog_tinyblog as tb, blog_tbcomment as tbc
    Where tb. num = tbc. num_tiny_blog

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

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

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

  1. I don't understand what you mean.

  2. What do you want to do.

  3. First, you do not need to query all links.

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

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.