小编典典

按存储过程参数排序

sql

我通过用户给定的参数对存储过程进行排序时遇到问题,我尝试阅读,但是尝试过的解决方案不起作用。

那么,有人可以帮助我吗?我希望可以从Group_concat中按类型对它进行排序。如果我以Harcoded形式输入它(例如,ORDER BY
Type1,ORDER BY Type2等),它将起作用。但是我想要这样的东西(ORDER BY @specificStat)。

CREATE PROCEDURE getSpecificStatsBySeason(IN khID INT(11), IN seasonName varchar(40), IN specificStat INT(11))
BEGIN

SET @sql = NULL;
SET @khID = khID;
SET @seasonName = seasonName;
SET @specificStat = specificStat;

SELECT
  GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(em.eventTypeID = ',
      et.eventTypeID, ', 1, 0)) AS Type',
      et.eventTypeID )
  ) INTO @sql
FROM  eventTypes       AS et, `eventType-R-kh` AS eRk, kh AS kh WHERE et.eventTypeID = eRk.eventTypeID AND eRk.kh = kh.kh AND kh.khID = @khID;

SET @sql = CONCAT('SELECT u.userID, ud.firstname, ud.lastname,', @sql,
                  ',
                  IFNULL((SELECT COUNT(s.activityID)
                          FROM activity AS a1, signup AS s
                          WHERE u.userid = s.userid
                          AND s.activityID = a1.activityID
                          AND a1.khID = @khID
                            AND s.answer = 1
                            AND se.seasonName = @seasonName
          AND a1.activityDate BETWEEN se.seasonStart AND se.seasonEnd
                  AND a1.activityDate <= NOW()), 0) AS matchCount
                  FROM userData AS ud, userInfo AS u
                  LEFT JOIN activity      AS  a ON a.khID = @khID
                  RIGHT JOIN season AS se ON se.seasonName = @seasonName
          AND a.activityDate BETWEEN se.seasonStart AND se.seasonEnd
                  LEFT JOIN `eventMatch`       AS em ON em.userid = u.userid
                  AND em.activityID = a.activityID
                  LEFT JOIN activityMatch AS am ON a.activityID = am.activityID 
                  WHERE u.khID  = @khID
                  AND u.userID = ud.userID
                  GROUP BY u.UserID  
                  ORDER BY  @specificStat  ;');


prepare stmt 
FROM @sql;

execute stmt;

阅读 194

收藏
2021-04-28

共1个答案

小编典典

改用这个:

...
...
  AND u.userID = ud.userID
              GROUP BY u.UserID  
              ORDER BY ',   @specificStat, '  ;');
2021-04-28