简体   繁体   English

使用来自一个mysql的结果进行第二次查询

[英]using results from one mysql for a second query

I have two tables: 我有两个表:

  • posts - holds post information 帖子-保存帖子信息
  • listen - holds information on what other users you are listening to. 收听-包含有关您正在收听的其他用户的信息。 (what users posts you want to view) (您要查看的用户帖子)

The structure of listen is: 监听的结构为:

  • id(uniqueid) ID(UNIQUEID)
  • userid(the users unique id) userid(用户唯一ID)
  • listenid(id of a user they are listening too) listenid(他们也在听的用户的ID)

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.

相关问题 使用来自一个MySQL查询的结果插入到另一个查询中 - Using results from one MySQL query to insert into another query 在PHP Envirnment中使用来自另一个查询中的一个MySQL查询的结果 - Using results from one MySQL query in another query in a PHP Envirnment 使用来自第一个MySQL查询的结果从第二个表中提取数据 - Extract data from second table using results from first MySQL query 有没有一种方法可以查询一个MySQL数据库两次,并将第二个查询的结果添加到与第一个查询的结果相同的数组中? - Is there a way to query a MySQL DB twice and add the results from the second query to the same array as the results from the first query? MySQL在查询中使用其他查询的结果吗? - MySQL In query using results from another query? MYSQL:查询两个表并将第二个表中的结果连接到一个数组 - MYSQL: Query two tables and join results from second table to an array 如何将一个查询的结果传递到第二个查询并在一个表中显示结果? - How can I pass results from one query into a second query and display the results in one table? 在第二个查询和嵌套结果中使用第一个mysqli查询中的数据 - Using data from first mysqli query in second query and nesting results 使用regexp从mysql查询中排除结果 - Exclude results from a mysql query using regexp 使用PHP从MySQL检索查询结果 - Retrieving results for query from MySQL using PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM