在mysql查询中放置WHERE的位置

时间:2013-11-04 21:31:52

标签: mysql

我找不到可以在查询中放置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 . "'

3 个答案:

答案 0 :(得分: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

您可以在the MySQL manual中看到此订单。

答案 1 :(得分: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

答案 2 :(得分:-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