1

我找不到可以在查询中放置 WHERE 子句的位置。似乎我尝试了所有可能的位置,但没有任何效果。

SELECT res
FROM (
SELECT `date`,SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE) AS res
FROM royalties 
GROUP BY `date`
ORDER BY `date` DESC LIMIT 12
) a
ORDER BY `date` ASC
WHERE `BUNDLE_ARTIST` = '" . $artist_name . "'
4

3 回答 3

6

我以为您需要将其放在FROM子查询中的子句之后:

SELECT res
FROM (
SELECT `date`,SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE) AS res
FROM royalties 
WHERE `BUNDLE_ARTIST` = '" . $artist_name . "'
GROUP BY `date`
ORDER BY `date` DESC LIMIT 12
) a
ORDER BY `date` ASC

你可以在MySQL 手册中看到这个顺序。

于 2013-11-04T21:33:52.320 回答
1

您似乎在子句中使用了内部查询。你犯了一些错误。尝试这个:

SELECT res
FROM 
(
    SELECT `date`,SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE) AS res
    FROM royalties 
    WHERE `BUNDLE_ARTIST` = '" . $artist_name . "'
    GROUP BY `date`
    ORDER BY `date` DESC LIMIT 12
) inner query
ORDER BY `date` ASC
于 2013-11-04T21:42:25.763 回答
-4

通常,我发现在 ORDER 和 GROUP 子句之前包含您的 WHERE 子句是最好的:

SELECT res
FROM (
SELECT `date`,SUM(DISTRIBUTED_AMOUNT / EXCHANGE_RATE) AS res
FROM royalties 
GROUP BY `date`
ORDER BY `date` DESC LIMIT 12
)
WHERE `BUNDLE_ARTIST` = '" . $artist_name . "'
ORDER BY `date` ASC
于 2013-11-04T21:34:32.493 回答