2011-08-18 93 views
0

我看着其他人有同樣問題的答案,但我無法弄清楚如何解決「不唯一表/別名」。MySQL不是唯一的表/別名

SELECT m.*, u.* 
    FROM ".TABLE_PREFIX."users_medals u 
    LEFT JOIN ".TABLE_PREFIX."medals m ON u.medal_id = m.medal_id 
    WHERE u.user_id IN (".$post['uid'].") 
    AND m.medal_level = (
    SELECT MAX(".TABLE_PREFIX."medals.medal_level) FROM ".TABLE_PREFIX."medals 
    LEFT JOIN ".TABLE_PREFIX."medals ON ".TABLE_PREFIX."users_medals.medal_id = ".TABLE_PREFIX."medals.medal_id 
    WHERE ".TABLE_PREFIX."users_medals.user_id = u.user_id 
    AND ".TABLE_PREFIX."medals.medal_type = m.medal_type 
) 

TABLE_PREFIX是我的腳本用來定義數據庫表前綴的東西。

+0

可能重複[MySQL的&PHP - 不是唯一的表/別名】(http://stackoverflow.com/questions/2077355/mysql-php-not-unique-table -alias) – Phil

+0

你究竟在哪裏尋找非唯一表/別名? – escproxy

回答

1

在你的子查詢

LEFT JOIN 「.TABLE_PREFIX。」 獎牌

我認爲這應該是

LEFT JOIN ".TABLE_PREFIX."users_medals 
+0

Yupp,工作!謝謝 :) – Spencer

0

子查詢你有兩個medals表,而不用別名。我認爲FROM ".TABLE_PREFIX."medals應該是FROM ".TABLE_PREFIX."users_medals

SELECT m.*, u.* 
    FROM ".TABLE_PREFIX."users_medals u 
    LEFT JOIN ".TABLE_PREFIX."medals m ON u.medal_id = m.medal_id 
    WHERE u.user_id IN (".$post['uid'].") 
    AND m.medal_level = (
    SELECT MAX(".TABLE_PREFIX."medals.medal_level) FROM ".TABLE_PREFIX."users_medals 
    LEFT JOIN ".TABLE_PREFIX."medals ON ".TABLE_PREFIX."users_medals.medal_id = ".TABLE_PREFIX."medals.medal_id 
    WHERE ".TABLE_PREFIX."users_medals.user_id = u.user_id 
    AND ".TABLE_PREFIX."medals.medal_type = m.medal_type 
) 
相關問題