Home > Backend Development > PHP Tutorial > 多对多的查询,三表查询,求mysql语句,该怎么处理

多对多的查询,三表查询,求mysql语句,该怎么处理

WBOY
Release: 2016-06-13 12:15:02
Original
757 people have browsed it

多对多的查询,三表查询,求mysql语句

本帖最后由 setoy 于 2015-02-06 15:11:52 编辑 普通应用:文章和标签的数据表,然后查询某个标签,如“技术”,能够查询出所有技术类的文章。

文章表article: 
aid, title,content<br />-----------------------------------------<br />1   Qt助力跨平台应用开发,势不可挡<br />2   Qt助力跨平台应用开发,势不可挡<br />3   Qt助力跨平台应用开发,势不可挡<br />4   Qt助力跨平台应用开发,势不可挡
Copy after login


标签表tags:
tid,tname<br />--------------------------------<br />1   生活<br />2   技术<br />3   科技<br />4   娱乐<br />
Copy after login


文章标签关系表art_tags:
aid,tid<br />----------------------------<br />1 1<br />1 2<br />1 3<br />2 1<br />2 3<br />2 4<br />3 1<br />3 2<br />3 4<br />4 1<br />4 2
Copy after login


然后根据标签搜索关键词“技术”,应该显示1、3、4这三篇文章
------解决思路----------------------
select a.* from article as a left join art_tags at on at.aid=a.aid where at.tid=2
------解决思路----------------------
<br />select * from article where aid in(select aid from art_tags where tid in(select tid from tags where tname='技术'));<br />
Copy after login

------解决思路----------------------
select a.* from article a <br />left join art_tags b on a.aid=b.aid <br />left join tags c on c.tid=b.tid where b.tname='技术'
Copy after login
 
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template