I have a sample data
game(id, name)
1 | Avatar
2 | Angry Bids
brand(id, parent_id, name)
1 | 0 | Apple
2 | 0 | Samsung
3 | 1 | Iphone 4
4 | 2 | Galaxy Tab
game_brand(game_id, brand_id)
1 | 4
2 | 3
And query:
SELECT game.name,
game_brand.brand_id,
(SELECT brand.parent_id
FROM brand AS brand
WHERE brand.brand_id = game_brand.brand_id) AS brand_father_id
FROM game AS game
LEFT JOIN game_brand AS game_brand
ON game_brand.game_id = game.id
WHERE game.id = 2
AND result:
name | brand_id | brand_father_id
Angry Bids | 3 | 1
How to get the values from brand_id, brand_father_id, eg:
game | brand | brand parent
Angry Bids | Iphone 4 | Apple
UPDATED
UPDATED AGAIN
AND YET AGAIN just now noticed that the OP wanted not just the fathers name, but also the child one’s (and not their ids also), so updated them both…