使用MySQL

时间:2018-07-18 18:31:50

标签: php mysql sql

我有名为

的表
**Subscription Table**
-------------------------------
id    actors_id      user_id
-------------------------------
1       5               1
2       7               2
3       12              96
4       18              54

**Actors Content**
-------------------------------
id    contend_id       actors_id
-------------------------------
1      25               5
2      65               18
3      120              18
4      98               12

**Content Table**
-------------------------------
r_id     title       content_post
-------------------------------
25     abvg         xxxxxxxxx
65     djki         fffffffff
98     sdrt         rrrrrrrrr
120    fgty         tttttttty

所以我首先需要从订阅表中获取actors_id,因为我知道user_id的值,之后需要从Actors内容表content_id中获取,最后从内容表中获取r_id并在网站上显示。

我尝试了一些操作,但这绝对不是很好的查询

SELECT Content.*, (SELECT Actors.contend_id FROM Actors WHERE Actors.contend_id = Content.r_id) as views FROM Content,Actors WHERE Actors.actors_id IN (SELECT Subscription.actors_id FROM Subscription WHERE Subscription.user_id = 96)

1 个答案:

答案 0 :(得分:1)

我想等效的联接查询看起来像

SELECT c.*, a.contend_id as views 
FROM Content c
JOIN Actors a ON a.contend_id = c.r_id
JOIN Subscription s ON a.actors_id  = s.actors_id
WHERE s.user_id = 96

也不要使用旧语法连接表,而是使用带有join关键字的显式语法

相关问题