ian
ian

Reputation: 12335

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?

Upvotes: 0

Views: 321

Answers (4)

henrikpp
henrikpp

Reputation: 348

a simple join wont work?

select 
 posts.* 
from 
 posts
inner join 
 listen
on 
 listen.listenID = posts.userID
where 
 listen.userID = ACTIVEUSER

Upvotes: 0

Roee Adler
Roee Adler

Reputation: 33980

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.

Upvotes: 0

cgp
cgp

Reputation: 41381

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.

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425251

SELECT  posts.*
FROM    listen
JOIN    posts
ON      posts.userid = listen.listenid
WHERE   listen.userid = @current_user

Upvotes: 1

Related Questions