繁体   English   中英

MySQL 中的 Select 条目

[英]Select entries in MySQL

有3张桌子

消息

+---------+---------------+---------------+--------------+-----------+--------+
| news_id |     title     |     short     |     body     | photo_id  | etc... |
+---------+---------------+---------------+--------------+-----------+--------+
|  881971 | Article Title | Article short | Article Body |    658998 |        |
|  881972 | Article Title | Article short | Article Body |    658999 |        |
+---------+---------------+---------------+--------------+-----------+--------+

学期

+-----+--------------+
| tid | news_term_id |
+-----+--------------+
|  14 |       881971 |
|   2 |       881972 |
|   2 |       881973 |
+-----+--------------+

照片

+--------+----------------------------------------+
|   id   |                  path                  |
+--------+----------------------------------------+
| 658998 | /files/2015/2/14304641562238139741.JPG |
| 658999 | /files/2015/2/14304641562238139742.JPG |
+--------+----------------------------------------+

我需要 select 表News中的所有文章,其中Term中的tid等于 2(例如)并包括来自Photo表的照片路径

我的查询:

select n.*, t.*
from news n
inner join term t 
    on n.news_id = t.news_term_id 
    and t.tid = 2

您当前的查询很好,您只需要带入photo表即可。 只需再添加一个联接:

select n.*, p.path
from term t
inner join news n on n.news_id = t.news_term_id
inner join photo p on p.phto_id = n.photo_id
where t.tid = 2

注意:我从term表开始查询,因为我发现它使逻辑更清晰(过滤条件t.tid = 2转到where子句)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM