选择列表;这与我的sql中的DISTINCT不兼容

时间:2017-04-24 09:26:03

标签: mysql

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAllEventIdList_SP`(in skillId varchar(50),in offsetvalue int,in recordlimit int,out totalcountquery text)
BEGIN

 set @whereClause="   ";

 SET @skills=concat('"',skillId,'"');

 if(skillId is not null and skillId <>"" and skillId <>0) then
 set @whereClause=concat(@whereClause," where skill_id in (",@skills ,")");

 end if;
     set totalcountquery=concat("select count(distinct (eventId)) from EventAttachmentList_View ",@whereClause);

  if(offsetvalue<recordlimit) then  
     set @SqlQuery=concat("select distinct (eventId),scheduleId from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc ", " limit " ,recordlimit , " offset 0");
     PREPARE querystatement FROM @SqlQuery;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;

  else 

     set @SqlQuery=concat("select distinct (eventId),scheduleId from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc  ",  " limit " ,recordlimit , " offset ", offsetvalue);

     PREPARE querystatement FROM @SqlQuery;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;

 end if;


END

当我调用数据调用tju.GetAllEventIdList_SP('1',17,16,@ totalcountquery)时,这是我的动态查询;  然后我得到错误 ORDER BY子句的表达式#1不在SELECT列表中,引用列'EventAttachmentList_View.scheduleDate'不在SELECT列表中;这与DISTINCT

不兼容

请建议我如何解决此问题在Query

中需要进行哪些更改

3 个答案:

答案 0 :(得分:0)

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAllEventIdList_SP`(in skillId varchar(50),in offsetvalue int,in recordlimit int,out totalcountquery text)
BEGIN

 set @whereClause="   ";

 SET @skills=concat('"',skillId,'"');

 if(skillId is not null and skillId <>"" and skillId <>0) then
 set @whereClause=concat(@whereClause," where skill_id in (",@skills ,")");

 end if;
     set totalcountquery=concat("select count(distinct (eventId)) from EventAttachmentList_View ",@whereClause);

  if(offsetvalue<recordlimit) then  
     set @SqlQuery=concat("select distinct (eventId),scheduleId from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc ", " limit " ,recordlimit , " offset 0");
     PREPARE querystatement FROM @SqlQuery;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;

  else 

     set @SqlQuery=concat("select distinct (eventId),scheduleId from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc  ",  " limit " ,recordlimit , " offset ", offsetvalue);

     PREPARE querystatement FROM @SqlQuery;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;

 end if;


END;

CALL GetAllEventIdList_SP('1',17, 16, @totalcountquery);

我曾经尝试过。它工作正常。enter image description here

答案 1 :(得分:0)

在LIMIT和OFFSET参数之间添加逗号。查询应该是这样的:

...LIMIT 16, OFFSET 17

此外,优化您的代码,尝试使用此代码而不是if(offsetvalue<recordlimit)... -

SET @offset = IF(offsetvalue < recordlimit, 0, offsetvalue);
SET @SqlQuery=concat("select distinct (eventId),scheduleId from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc  ",  " limit " ,recordlimit , ", offset ", @offset);
PREPARE querystatement FROM @SqlQuery;
EXECUTE querystatement;
DEALLOCATE PREPARE querystatement;

答案 2 :(得分:0)

您的MySQL版本启用了ONLY_FULL_GROUP_BY模式,这意味着(在MySQL 5.7.5及更高版本中)使用DISTINCTORDER BY的任何查询都将要求ORDER BY中的列也位于查询的SELECT部分。

修复方法是将scheduleDate添加到查询的SELECT部分,如下所示:

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAllEventIdList_SP`(in skillId varchar(50),in offsetvalue int,in recordlimit int,out totalcountquery text)
BEGIN

 set @whereClause="   ";

 SET @skills=concat('"',skillId,'"');

 if(skillId is not null and skillId <>"" and skillId <>0) then
 set @whereClause=concat(@whereClause," where skill_id in (",@skills ,")");

 end if;
     set totalcountquery=concat("select count(distinct (eventId)) from EventAttachmentList_View ",@whereClause);

  if(offsetvalue<recordlimit) then
     set @SqlQuery=concat("select distinct (eventId),scheduleId,scheduleDate from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc ", " limit " ,recordlimit , " offset 0");
     PREPARE querystatement FROM @SqlQuery;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;

  else

     set @SqlQuery=concat("select distinct (eventId),scheduleId,scheduleDate from EventAttachmentList_View ",@whereClause,  " order by scheduleDate desc  ",  " limit " ,recordlimit , " offset ", offsetvalue);

     PREPARE querystatement FROM @SqlQuery;
     EXECUTE querystatement;
     DEALLOCATE PREPARE querystatement;

 end if;


END