MySQL语法错误,但一切看似正确

时间:2012-01-21 18:08:04

标签: mysql sql syntax

这是SQL错误:

Error: 1064 - You have an error IN your SQL syntax; CHECK the manual that corresponds TO         your MySQL server version FOR the RIGHT syntax TO USE near ') and (pi.perm_type='forum' and ( pi.perm_2='*' OR pi.perm_2 LIKE '%,4,%' )) and' at line 2

这是SQL查询:

SELECT t.tid AS id, t.title, t.last_poster_id AS id2,m.member_group_id, 
m.members_display_name AS statistic,pi.perm_2 FROM topics t  LEFT JOIN members m ON (
m.member_id  = t.last_poster_id )
LEFT JOIN permission_index pi ON ( pi.app='forums' AND pi.perm_type='forum' AND 
pi.perm_type_id=t.forum_id )   WHERE t.forum_id NOT IN() AND (pi.perm_type='forum' AND (
pi.perm_2='*' OR pi.perm_2 LIKE '%,4,%' )) AND t.approved=1 AND (t.moved_to='' OR 
t.moved_to IS NULL ) ORDER BY t.last_post DESC LIMIT 0,11

现在,这个查询在我的MySQL本地安装(XAMPP)上运行良好。

可能是因为不同的MySQL版本会在此查询中导致SQL错误吗?究竟是什么导致了错误?

  1. “LIKE”运算符?

  2. where子句中的嵌套语句? (在())之间

  3. 还有别的吗?

  4. 提前致谢!

4 个答案:

答案 0 :(得分:4)

你有一个空的NOT IN

此外,您不需要pi上的WHERE条件之一,因为它位于LEFT JOIN中。但是,外表pi上的WHERE中的LIKE过滤器将其更改为内连接。

所以,你需要这个并修复你的IN

SELECT 
   t.tid AS id, 
   t.title, 
   t.last_poster_id AS id2,m.member_group_id, 
   m.members_display_name AS statistic,  
   pi.perm_2 
FROM 
   topics t
   LEFT JOIN
   members m ON m.member_id  = t.last_poster_id
   LEFT JOIN 
   permission_index pi ON pi.app='forums' AND pi.perm_type='forum' AND 
                                    pi.perm_type_id=t.forum_id
                             AND 
                          (pi.perm_2='*' OR pi.perm_2 LIKE '%,4,%')
WHERE 
   -- t.forum_id NOT IN() -- error is here
   -- AND 
   t.approved=1 
   AND 
   (
    t.moved_to='' OR t.moved_to IS NULL) 
ORDER BY 
   t.last_post DESC 
LIMIT 0,11

当您注意格式化时会更容易,是吗?

答案 1 :(得分:3)

 t.forum_id NOT IN()

IN子句中没有值。

答案 2 :(得分:1)

条件“t.forum_id NOT IN()”无效,因为IN要求至少定义一个值。

答案 3 :(得分:0)

我不熟悉您用于LIKE的语法。它可能需要从LIKE'%,4,%'更改为LIKE'%4%'。你试过这个吗?