[英]using results from one mysql for a second query
I have two tables: 我有两个表:
The structure of listen is: 监听的结构为:
How would I gather all the entries from listen that mach the active users userid and then use those to find all the posts that match any of the found listenid values so as to create a query of the combined users posts I want to view? 我将如何从侦听中收集所有活跃用户userid的条目,然后使用这些条目查找与找到的任何listenid值匹配的所有帖子,以便创建要查看的组合用户帖子的查询?
SELECT posts.*
FROM listen
JOIN posts
ON posts.userid = listen.listenid
WHERE listen.userid = @current_user
You can do this with a simple natural join, or a direct join as given in other answers. 您可以使用简单的自然联接,也可以使用其他答案中给出的直接联接。
select
*
from
posts, listen
where
listen.userid == $active_user and
posts.userid = listen.userid
You probably want to be more selective about the columns you are bringing in. 您可能希望对要引入的列更具选择性。
I think you're talking about something like this: 我认为您是在谈论这样的事情:
select postid from posts
where userid in
(
select listenid from listen
where userid = CURRENT-USER
)
This is assuming the table posts has a userid field. 假设表中的帖子有一个userid字段。
a simple join wont work? 一个简单的连接将无法正常工作?
select
posts.*
from
posts
inner join
listen
on
listen.listenID = posts.userID
where
listen.userID = ACTIVEUSER
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.