0

这是我的查询,它使 Join 出错

        $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 服务器版本相对应的手册,以'JOIN T_USER_ACCOUNT b ON b.u_id = a.ks_u_id ORDER BY a.ks_time DESC '在第 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 命令编写了错误的查询。

4

2 回答 2

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
于 2012-05-22T08:36:49.417 回答
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
于 2012-05-22T08:42:16.013 回答