区别在于MySQL中的程序不起作用

时间:2016-12-06 04:52:58

标签: mysql sql

    if(eventType=1  ) then
     set @Whereclause=CONCAT(@Whereclause," and  scheduleDate > ",@currentdate, " order by  scheduleDate " );
    end if;
     if(eventType=0  ) then
     set @Whereclause=CONCAT(@Whereclause," and  scheduleDate < ",@currentdate," order by  scheduleDate  desc " );
    end if;
     if(eventType=-1 ) then 
      set @Whereclause=CONCAT(@Whereclause," order by  scheduleDate  desc  " );
      end if;

   set @SqlQuery=  CONCAT("
select   distinct(event_schedule_id)
 from TEST_EVENT_LIST_VIEW ", @Whereclause );


set totalcountquery=@Whereclause;

  if(offsetvalue<recordlimit) then  

      set @QueryfforPagination=CONCAT(@SqlQuery," limit ",recordlimit );
     PREPARE querystatement FROM @QueryfforPagination;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;


    else 
       set  @QueryfforPagination=CONCAT(@SQLQuery," limit ",recordlimit," offset ",offsetvalue);
       PREPARE querystatement FROM @QueryfforPagination;
       EXECUTE querystatement;
       DEALLOCATE PREPARE querystatement;

    end if;


END

当我尝试使用select distinct(event_schedule_id)执行此过程时,这是我的过程查询,它显示错误

  

错误代码:3065。ORDER BY子句的表达式#1不在SELECT中   list,references column&#39; TEST_EVENT_LIST_VIEW.scheduleDate&#39;是的   不在SELECT列表中;这与DISTINCT

不兼容

当我删除不同然后其工作正常时,请建议我解决此问题的人。

1 个答案:

答案 0 :(得分:0)

在使用DISTINCT和ORDER BY时,我们必须在SELECT列表中有ORDER BY列,如下所示:

<强>要么

 order by  event_schedule_id desc

set @SqlQuery=  CONCAT("
select   distinct event_schedule_id, scheduleDate
from TEST_EVENT_LIST_VIEW ", @Whereclause );

SELECT DISTINCT A FROM myTable ORDER BY B --It doesn't work
SELECT DISTINCT A FROM myTable ORDER BY A --It works
SELECT DISTINCT A, B FROM myTable ORDER BY A, B --It works
SELECT DISTINCT A, B FROM myTable ORDER BY B --It works

如果我们从上面删除DISTINCT,那么所有语句都可以正常工作。