【发布时间】:2019-10-27 23:39:54
【问题描述】:
有 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 |
+--------+----------------------------------------+
我需要选择 News 表中的所有文章,其中 Term 中的 tid 等于 2(例如)并包含来自 的照片路径照片表格
我的查询:
select n.*, t.*
from news n
inner join term t
on n.news_id = t.news_term_id
and t.tid = 2
【问题讨论】:
-
我们如何关联表
photo和news?我们可能需要在表photo中像news_id这样的列。 -
在 News 表中有 photo_id 列,在 Photo 列中有 id 列等于 photo_id
标签: mysql sql phpmyadmin