用select * from tab  where number = '333333' 和
select max(loop) from tab where number = '333333'
都可以查出数据
但用下面联合在一起就报语法错误。菜鸟请教为什么?select * from tab  where number = '333333' 
and loop in( select max(loop) from tab where number = '333333')

解决方案 »

  1.   

    select * 
    from `tab`
    where `number`='333333'
    and `loop` in ( select max(`loop`) from tab where `number`='333333');
    是不是太多关键字了。 摄氏信息是什么?
      

  2.   

    select * from `tab`  where `number` = '333333' 
    and `loop` in( select max(loop) from `tab` where `number` = '333333')
      

  3.   

    我试了一下,没有问题啊select * from product  where category = 'computer' 
    and productid in( select max(productid) from product where category = 'computer')
      

  4.   

    这个我在5.1.30中尝试
    create table tab(number char(8), loop int(11));    
    ERROR 1064 (42000): 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 'loop int(11))' at line 1
    loop是关键字吧!!
    mysql> create table tab(number char(8), lop int(11)); 
    Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql> insert into tab values('333333', 1),('333333',2)
        -> ,('333333',3);
    Query OK, 3 rows affected (0.00 sec)
    mysql> select * from tab  where number = '333333'  and lop in( select max(lop) from tab where number = '333333'); 
    +--------+------+
    | number | lop  |
    +--------+------+
    | 333333 |    3 | 
    +--------+------+
    1 row in set (0.01 sec)
      

  5.   

    保留字的问题。楼主下次有问题的时候请尽可能多的提供信息,比如错误信息。mysql> select * from tab;
    +----+--------+------+
    | id | number | loop |
    +----+--------+------+
    |  1 | 333333 |   10 |
    |  2 | 333333 |   60 |
    |  3 | 333333 |   40 |
    |  4 | 333333 |   20 |
    +----+--------+------+
    4 rows in set (0.00 sec)mysql>
    mysql> select *
        -> from `tab`
        -> where `number`='333333'
        -> and `loop` in ( select max(`loop`) from tab where `number`='333333');
    +----+--------+------+
    | id | number | loop |
    +----+--------+------+
    |  2 | 333333 |   60 |
    +----+--------+------+
    1 row in set (0.00 sec)mysql> select * from tab  where number = '333333'
        -> and loop in( select max(loop) from tab where number = '333333')
        -> ;
    ERROR 1064 (42000): 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 'loop
    in( select max(loop) from tab where number = '333333')' at line 2
    mysql>
      

  6.   


    select * from product  where category = 'computer' 
    and productid in( select max(productid) from product where category = 'computer')
    我分别在MYSQL和MSSQL中都查询了没有问题啊
      

  7.   

    你换成别的字段试试,可能loop是关键字,你的sql语句没有问题
      

  8.   

    我看了loop  不是关键字。我MySQL 是4.0的我的错误提示:
    mysql> select * from `tab`  where `number` = '333333'
        -> and `loop` in( select max(`loop`) from `tab` where
    `number` = '333333') ;
    ERROR 1064: You have an error in your SQL syntax.  Check the manual that corresp
    onds to your MySQL server version for the right syntax to use near 'select max(
    `loop`) from `tab` where `number`
    mysql>
      

  9.   

    http://dev.mysql.com/doc/refman/5.1/zh/introduction.html#roadmap1.6. MySQL发展大事记
    特性
     MySQ系列
     
    Foreign keys
     3.23(针对InnoDB存储引擎)
     
    Unions
     4.0
     
    Subqueries
     4.1
     
      

  10.   

    试试这句吧,相同的效果,取出LOOP最大的一行。select *                                                              
    from `tab` a                                                           
    where `number`='333333'  
    and not exists (select 1 from tab where `number`='333333' and `loop`>a.`loop`);
      

  11.   

    在MYSQL 5.1下测试,SQL语句没有问题
    select * from `tab`  where `number` = '333333' and `loop` in ( select max(`loop`) from `tab` where
    `number` = '333333') ;这样:
    select * from `tab`  where `number` = '333333' and `loop`=( select max(`loop`) from `tab` where `number` = '333333') ;OR
    select * from `tab` A 
    INNER JOIN
    (select max(`loop`) AS MA from `tab` where `number` = '333333') B
    ON A.`LOOP`=B.MA
    where A.`number` = '333333'