我有一个样本数据
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
并查询:
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
和结果:
name | brand_id | brand_father_id
Angry Bids | 3 | 1
如何从brand_id
,中获取值brand_father_id
,例如:
game | brand | brand parent
Angry Bids | Iphone 4 | Apple