2013-11-04 167 views
1

我找不到我的WHERE子句在我的查詢。這似乎是我嘗試了所有可能的位置,但沒有任何工作。哪裏可以放在哪裏在MySQL查詢

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 . "' 
+1

@AlephDvorak這是在他的頭銜和標籤 – Barmar

+2

順便指出,由''判斷建設我認爲你是在PHP這樣做。我希望你正確地轉義變量值,否則我可能會試圖搜索我最喜歡的藝術家''; DELETE FROM royalty; SELECT「:-) 檢出[mysql-escape-string](http ://php.net/manual/en/function.mysql-escape-string.php)。 – CompuChip

+0

除了@CompuChip說了什麼之外,我建議查看[預先準備的語句](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php),特別是最後的部分[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)的保護。 –

回答

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記錄。

-4

通常情況下,我覺得包括你在ORDER和GROUP子句前子句是最好的:

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 
+3

這不起作用,子查詢不會返回BUNDLE_ARTIST列。 – Barmar

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