MySQL get header from parent with two tables

I will try to explain as much as possible. here is my request ..

SELECT * FROM mm_star_ratings s
JOIN mm_posts p ON s.post_id = p.postid
WHERE p.type='B'
ORDER BY s.rating DESC LIMIT 5

      

type='B'

is the main post and type='C'

is the answer to this post. There mm_posts

is a column parentid

to set the main post (B)

id for C

. I want to get title

from mm_posts

if type='C'

all the same?

The challenge for me is to get it to work with my rating table where post_id

both are set for B

and forC

+3


source to share


1 answer


You can use the following query,



SELECT s.*,p.*, IF(p.type='C',pp.title,p.title) as title FROM mm_star_ratings s
JOIN mm_posts p ON s.post_id = p.postid Left Join mm_posts pp on pp.postid=p.parent_id 
ORDER BY s.rating DESC LIMIT 5

      

+4


source







All Articles