2012-07-14 48 views
0

這是針對我正在開發的遊戲。 我有一個MySQL表,其中包含他們雙手各握例如武器的ID每個球員實體:針對遊戲應用的特定mysql查詢

player id, playername, rhand, lhand, 
1   Guy   1  2 
2   OtherGuy 3  2 

,我有通過ID包含每個項目的信息項目表

item name damage_type 
1  hammer crush 
2  shield none 
3  sword slash 

在我的服務器上解決戰鬥時,我通過id搜索每個玩家一個是攻擊者,一個是防禦者我想返回一個結果,每個玩家的武器名稱和武器屬性(即上面的damage_type)相同的行,所以我想要這樣的結果

player id playername rhand lhand rhand_damage_type lhand_damage_type 
1   Guy  hammer shield crush    none 
2   OtherGuy sword shield slash    none 
+0

SELECT * FROM game_moblist LEFT JOIN項目ON(= RH和items.id)LEFT JOIN項目開(lhand =項目。 ID)WHERE 1 這沒有工作,因爲即時通訊使用相同的別名爲左和右 – Shawn 2012-07-14 13:56:18

回答

0
SELECT p.player_id, p.player_name, w1.name, w2.name, w1.damage_type, w2.damage_type 
FROM players p, weapons w1, weapons w2 
WHERE p.player_id = attackerId OR p.player_id=defenderId 
AND p.rhand = w1.item 
AND p.lhand = w2.item 

萬一plyers也可能會錯過一個或兩個武器:

SELECT DISTINCT player_id, player_name, w1.name, w2.name, w1.damage_type, w2.damage_type 
FROM 
(SELECT player_id, player_name, name, damage_type 
FROM players LEFT JOIN weapons on rhand = w1.item 
WHERE p.player_id = attackerId OR p.player_id=defenderId) as w1, 
(SELECT player_id, player_name, name, damage_type 
FROM players LEFT JOIN weapons on lhand = w2.item 
WHERE p.player_id = attackerId OR p.player_id=defenderId) as w2 
Were w1.player_id = w2.player_id 
+0

我試圖。 – Shawn 2012-07-14 14:02:41

+0

我得到空的結果我可能做錯了。我在想這個解決方案是rhand = item.id的左連接,第二個左連接是lhand = item.id,但是使用別名,我不知道這個語法足夠好。 – Shawn 2012-07-14 14:08:43

+0

嘗試編輯答案 – Razvan 2012-07-14 14:12:33