现在有流程记录表 t(序号  任务号  步骤审核角色  流程顺序号   审核状态)id  tid   role   orderid   status
1    3    CR         1          1
2    3    FS         2          0
3    3    CEO      3          0
4    2    CR        1          1
5    2    FS         2          1
6    2    CEO      3          0
7    4      CR      1          1
8    4      FS       2          0
9    4      CEO    3          0现在需要查出某用户角色 当前的审核数据我是这样写的,虽然是可以,但是效率比较低,最后面那句效率比较低,有没其他高效率的办法
select * from table t
       where t.typeid=2
       and t.role like concat('%','CEO','%')
       and t.status=0 
       and t.id = (
    ​    ​    ​    ​    ​    ​select t1.id from table t1  
    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​where t.tid=t1.tid
    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​and t1.status=0 
    ​    ​    ​    ​    ​    ​    ​    ​    ​order by t1.orderid asc limit 1)
       

解决方案 »

  1.   

    解决了,在tid加个索引。
      

  2.   

    这样实现行不行?如果需要经常弄的话,做成个存储过程调用。
    mysql> select tid,role from t where status=0 and tid=3 group by tid having max(orderid);
    +------+------+
    | tid  | role |
    +------+------+
    |    3 | FS   |
    +------+------+
    1 row in set (0.00 sec)mysql> select tid,role from t where status=0 and tid=2 group by tid having max(orderid);
    +------+------+
    | tid  | role |
    +------+------+
    |    2 | CEO  |
    +------+------+
    1 row in set (0.00 sec)mysql> select tid,role from t where status=0 and tid=4 group by tid having max(orderid);
    +------+------+
    | tid  | role |
    +------+------+
    |    4 | FS   |
    +------+------+
    1 row in set (0.00 sec)
      

  3.   

    这里3个SQL了, 我要一个SQL查出呢,不过我解决了,在TID 加个索引,就这么简单 
      

  4.   

    还可以注意,在where语句中,把更具有    唯一性  的 字段放在前面。