我必須編寫一個查詢來使用所選日期和所選類別從表中檢索數據。我寫了一個查詢,並沒有按預期提供正確的數據。如果action
列的值爲'AD'
,它應該只查詢最大爲actiondate
的行。具有最大和特定值的MySQL行
INSERT INTO goldstockvaluation SELECT sh.stockid, sh.description, sh.branch, sh.grossweight, sh.pureweight, sh.purity, sh.goldcarat, sh.mcpergram, sh.goldpergram, sh.fixgold, CURDATE()
FROM stock_history sh
JOIN (
SELECT stockid, branch,ACTION , MAX(actiondate) AS MaxDateTime
FROM stock_history
GROUP BY stockid,branch,ACTION
)groupedsh ON sh.stockid = groupedsh.stockid
AND sh.actiondate = groupedsh.MaxDateTime
AND sh.branch = groupedsh.branch
AND sh.action = groupedsh.action
AND sh.branch = '8'
AND sh.categoryid = 'G'
AND sh.action = 'AD'
AND sh.actiondate <= '2016-03-28 23:59:59'
這是查詢出有action
爲'AD'
,並有max(actiondate)
行。
什麼是主鍵?使用'SHOW CREATE TABLE stock_history'併發布結果。 –