以下是存储过程中的SQL语句,它返回记录正常。但是,我还需要MEMBER_ID的值。当我将它添加到select语句时,则不返回任何记录。我尝试过CAST和CONVERT,但没有结果。作为参考,字段MEMBER_ID是两个表中的键值。这是一个INT。
SELECT
CONCAT(WEBFSS.MEMBERS.LAST_NAME, ", " ,WEBFSS.MEMBERS.FIRST_NAME) As Name,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _mon THEN WEBFSS.SCHEDULE.QTY END) AS MEALS1,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _mon THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS1,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _tue THEN WEBFSS.SCHEDULE.QTY END) AS MEALS2,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _tue THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS2,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _wed THEN WEBFSS.SCHEDULE.QTY END) AS MEALS3,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _wed THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS3,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _thu THEN WEBFSS.SCHEDULE.QTY END) AS MEALS4,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _thu THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS4,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _fri THEN WEBFSS.SCHEDULE.QTY END) AS MEALS5,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _fri THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS5
FROM WEBFSS.SCHEDULE
INNER JOIN WEBFSS.MEMBERS
ON WEBFSS.SCHEDULE.MEMBER_ID = WEBFSS.MEMBERS.MEMBER_ID
WHERE
(WEBFSS.SCHEDULE.DATE BETWEEN _mon AND _fri)
GROUP BY
WEBFSS.MEMBERS.LAST_NAME, WEBFSS.MEMBERS.FIRST_NAME ;
这不起作用:
SELECT
SCHEUDLE.MEMBER_ID,
CONCAT(WEBFSS.MEMBERS.LAST_NAME, ", " ,WEBFSS.MEMBERS.FIRST_NAME) As Name,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _mon THEN WEBFSS.SCHEDULE.QTY END) AS MEALS1,
答案 0 :(得分:1)
尝试按SCHEUDLE.MEMBER_ID
和CONCAT(WEBFSS.MEMBERS.LAST_NAME, ", " ,WEBFSS.MEMBERS.FIRST_NAME)
分组:
SELECT
SCHEUDLE.MEMBER_ID,
CONCAT(WEBFSS.MEMBERS.LAST_NAME, ", " ,WEBFSS.MEMBERS.FIRST_NAME) As Name,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _mon THEN WEBFSS.SCHEDULE.QTY END) AS MEALS1,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _mon THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS1,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _tue THEN WEBFSS.SCHEDULE.QTY END) AS MEALS2,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _tue THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS2,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _wed THEN WEBFSS.SCHEDULE.QTY END) AS MEALS3,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _wed THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS3,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _thu THEN WEBFSS.SCHEDULE.QTY END) AS MEALS4,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _thu THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS4,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _fri THEN WEBFSS.SCHEDULE.QTY END) AS MEALS5,
SUM(CASE WHEN WEBFSS.SCHEDULE.DATE = _fri THEN WEBFSS.SCHEDULE.ENSURE END) AS ENS5
FROM WEBFSS.SCHEDULE
INNER JOIN WEBFSS.MEMBERS
ON WEBFSS.SCHEDULE.MEMBER_ID = WEBFSS.MEMBERS.MEMBER_ID
WHERE
(WEBFSS.SCHEDULE.DATE BETWEEN _mon AND _fri)
GROUP BY SCHEUDLE.MEMBER_ID, CONCAT(WEBFSS.MEMBERS.LAST_NAME, ", " ,WEBFSS.MEMBERS.FIRST_NAME);
当你在select中包含一个聚合函数时,如果没有包含在group by中的额外列,那么mysql将为那些既不包含在聚合函数中也不包含在group by子句中的列选择随机值。