Mysql选择具有相同id的行(3个表)

时间:2016-11-30 10:59:24

标签: mysql sql select

我有以下表格:

' blog_content'

blog_content

' blog_media'

blog_media

' blog_media_content'

* {font-family: 'Segoe UI'; margin: 0; padding: 0; list-style: none; line-height: 1; box-sizing: border-box;}
body {background-color: #f5f5f5;}
a {text-decoration: none; color: inherit;}

.mask {position: fixed; top: 0; bottom: 0; right: 0; left: 0; z-index: 8;}

nav > ul > li {display: inline-block; position: relative; width: 30%;}
nav > ul > li a {display: block; padding: 5px; border: 1px solid #ccc;}
nav > ul ul {position: absolute; left: 0; right: 0; z-index: 9; display: none;}
nav > ul > li.open > ul {display: block;}

我想选择<script src="https://code.jquery.com/jquery-2.2.4.js"></script> <div class="mask"></div> <nav> <ul> <li> <a href="">Main Item 1</a> <ul> <li><a href="">Sub Item 1</a></li> <li><a href="">Sub Item 2</a></li> <li><a href="">Sub Item 3</a></li> <li><a href="">Sub Item 4</a></li> <li><a href="">Sub Item 5</a></li> </ul> </li> <li> <a href="">Main Item 2</a> <ul> <li><a href="">Sub Item 1</a></li> <li><a href="">Sub Item 2</a></li> <li><a href="">Sub Item 3</a></li> <li><a href="">Sub Item 4</a></li> <li><a href="">Sub Item 5</a></li> </ul> </li> <li> <a href="">Main Item 3</a> <ul> <li><a href="">Sub Item 1</a></li> <li><a href="">Sub Item 2</a></li> <li><a href="">Sub Item 3</a></li> <li><a href="">Sub Item 4</a></li> <li><a href="">Sub Item 5</a></li> </ul> </li> </ul> </nav>等于| blog_id | media_id | ======================== | 1 | 1 | | 2 | 2 | | 3 | 3 | | 3 | 4 | 的所有blog_media.uri

请帮助我实现目标。

1 个答案:

答案 0 :(得分:0)

inner joinblog_media表之间的blog_media_content就足够了。

SELECT 
bm.uri 
FROM blog_media bm 
INNER JOIN blog_media_content bmc ON bm.media_id = bmc.media_id
WHERE bmc.blog_id =3; 

注意:

如果您需要blog表中的任何其他信息,那么您需要额外的inner join,如下所示:

...INNER JOIN blog_table b ON bmc.blog_id = b.blog_id...

修改

为了获取所有blog_id的记录:

SELECT 
bm.uri 
FROM blog_media bm 
INNER JOIN blog_media_content bmc ON bm.media_id = bmc.media_id
ORDER BY bmc.blog_id;