Multiple-to-many queries, three-table queries, and mysql statements

Source: Internet
Author: User
Multi-to-many queries, three-table queries, evaluate mysql statements for common applications: articles and tag data tables, and then query a tag, such as "technology", to query all technical articles.

Article table article:
Aid, title, content ------------------------------------- 1 Qt assists in cross-platform application development, 2 Qt assists in cross-platform application development, 3 Qt assists in cross-platform application development, and 4 Qt assists in cross-platform application development.


Label table tags:
Tid, tname -------------------------------- 1 life 2 technology 3 Technology 4 Entertainment


Article tag relationship table art_tags:
aid,tid----------------------------1 11 21 32 12 32 43 13 23 44 14 2


Then, search for the keyword "technology" based on the tag and display the articles 1, 3, and 4.


Reply to discussion (solution)

Select a. * from article as a left join art_tags at on at. aid = a. aid where at. tid = 2

Select a. * from article as a left join art_tags at on at. aid = a. aid where at. tid = 2



At. tid = 2? This cannot be known in advance. The user inputs the Chinese character "technology"

Select * from article where aid in (select aid from art_tags where tid in (select tid from tags where tname = ''));

Select a. * from article a left join art_tags B on a. aid = B. aid left join tags c on c. tid = B. tid where B. tname =''

Thank you!
The original where statement can also be written like this.

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.