MySQL Query relationship Query

Source: Internet
Author: User

Connection query: Select Table_b.type,table_b.title,table_b.content,table_b.author from table_a left join table_b on table_a.type = Table_b.type where table_a.id = 1;

Subquery: Select Type,title,content,author from table_b where type = (select type from table_a where id = 1);

If an ID corresponds to more than one type, use Select Type,title,content,author from Table_b where type in (select type from table_a where id = 1);

The results are as follows:

Using the most common query (not in the form of a relational table or an intermediate table) can also get the same effect, as follows:

SELECT * from table_b WHERE type = ' a ';

So why would it take so much effort to build a relational table?

The difference between the two approaches is that the second one queries all records of Class A directly in the detail table, and the first is obtained by the parameter ID to category A, and then to the detail table for all Class A records.

I personally believe that the reasons may include: 1, join the relationship table or the intermediate table, can make the relationship between the data more explicit; 2, the front-end is passed by an ID parameter, backstage only through this ID parameter to carry out a series of queries, to a certain extent, to ensure the security of the data.

MySQL Query relationship Query

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.