2012-05-22 125 views
0

這裏是我的查詢,使錯誤的加入MySQL查詢連接表不起作用

 $query = 'SELECT 
        a.ks_u_id, 
        a.ks_keyword, 
        b.u_photo 
       FROM 
       '.T_KEYWORD_HISTORY.' a 
       WHERE a.ks_u_id in (SELECT uf_target_id FROM '.T_USER_FOLLOW.' WHERE uf_user_id="'.$u_id.'" and uf_target_id <> "'.$u_id.'") 
       JOIN '.T_USER_ACCOUNT.' b ON b.u_id = a.ks_u_id 
       ORDER BY a.ks_time DESC 
       LIMIT 0 , 5 '; 

我收到此錯誤信息。

您的SQL語法有錯誤;檢查對應於你的MySQL服務器版本正確的語法在第8行

SELECT 
    a.ks_u_id, a.ks_keyword, b.u_photo 
FROM T_KEYWORD_HISTORY a 
WHERE a.ks_u_id in (SELECT uf_target_id 
        FROM T_USER_FOLLOW 
        WHERE uf_user_id="jake" and uf_target_id <> "jake") 
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id 
ORDER BY a.ks_time DESC 
LIMIT 0 , 5 

它看起來像我寫的JOIN命令錯誤的查詢附近'JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id ORDER BY a.ks_time DESC '使用手冊。

+4

WHERE子句必須在JOIN子句之後。 –

+0

哦..它現在有效。謝謝! – james

回答

0

嘗試

SELECT 
    a.ks_u_id, a.ks_keyword, b.u_photo 
FROM T_KEYWORD_HISTORY a 
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id 
WHERE a.ks_u_id in (SELECT uf_target_id 
        FROM T_USER_FOLLOW 
        WHERE uf_user_id="jake" and uf_target_id <> "jake") 
ORDER BY a.ks_time DESC 
LIMIT 0 , 5 
0

JOIN條款而來的WHERE子句之前。您的查詢應該是:

SELECT 
a.ks_u_id, a.ks_keyword, b.u_photo 
FROM T_KEYWORD_HISTORY a 
JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id 
WHERE a.ks_u_id in (SELECT uf_target_id 
       FROM T_USER_FOLLOW 
       WHERE uf_user_id="jake" and uf_target_id <> "jake") 
ORDER BY a.ks_time DESC 
LIMIT 0 , 5