在mysql的存储过程中发出Case Expression

时间:2017-04-29 08:34:41

标签: php mysql stored-procedures

我在mysql的存储过程中遇到问题。     主要是我面临CASE问题那些改变结果。但我想要根据调用选择结果如果我想根据id或名称或类别的表顺序。所以我实现了CASE Expression。但没有找到正确的结果

存储过程

CREATE PROCEDURE StudentTablePagination
(

      IN Class NVARCHAR(50) ,
      IN Name NVARCHAR(50),
      IN StartRecord INT,
      IN PageSize INT,
      IN SortColumn NVARCHAR(20),
      IN SortOrder NVARCHAR(4)
)
BEGIN
  IF SortOrder = 'ASC' 
  Then
     SELECT  id from lakes where name like CONCAT('%',Name,'%') AND  class like 
     CONCAT('%',Class,'%')
     ORDER BY  (
       CASE SortColumn
       WHEN 'name' THEN lakes.name
       WHEN 'state' THEN lakes.state
       ELSE id END
    ) Asc 
    LIMIT StartRecord, PageSize;

 ELSEIF SortOrder = 'DESC' 
 Then
    SELECT  id from lakes where name like CONCAT('%',Name,'%') AND  class like 
    CONCAT('%',Class,'%')
    ORDER BY  (
      CASE SortColumn
      WHEN 'name' THEN lakes.name
      WHEN 'state' THEN lakes.state
      ELSE id END
    )  DESC LIMIT StartRecord , PageSize;
 END IF;
END

呼叫:

CALL StudentTablePagination('','', 0,10,'id','asc')

和结果:

id
10
100
1000

但结果要求:

id
1
2
3

0 个答案:

没有答案