Multi-to-many queries, three-table queries, and mysql statements. how can this problem be solved?

Source: Internet
Author: User
Multi-to-many queries, three-table queries, mysql statement This post was last edited by setoy from 2015-02-0615: 11: 52 Common Application: Data tables of articles and labels, then query a tag, such as "technology", to query all technical articles. Article table article: & nbsp; aid, & nbsp; title, content ------------ multi-to-many queries, three-table queries, evaluate mysql statements

This post was last edited by setoy at 15:11:52 on

Common applications: Data tables of articles and tags, and then query a tag, such as "technology", to query all technical articles.

Article table article:
Aid, title, content
-----------------------------------------
1 Qt facilitates cross-platform application development, which is unstoppable
2 Qt assists in cross-platform application development, which is unstoppable
3 Qt assists in cross-platform application development, which is unstoppable
4 Qt assists in cross-platform application development, which is unstoppable


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


Article tag relationship table art_tags:
aid,tid
----------------------------
1 1
1 2
1 3
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2


Then, search for the keyword "technology" based on the tag and display the articles 1, 3, and 4.
------ Solution ----------------------
Select a. * from article as a left join art_tags at on at. aid = a. aid where at. tid = 2
------ Solution ----------------------

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

------ Solution ----------------------
Select a. * from article
Left join art_tags B on a. aid = B. aid
Left join tags c on c. tid = B. tid where B. tname = 'technical'

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.