我有一個表,列id,日期,estValue & gradeid。每個成績ID有大約12條記錄,總共約120條記錄有大約10個不同的成績[給與接受]。我需要從數據庫中創建一個選擇,它給我一個看起來像這樣的結果集:複雜嗎? MySQL Query
date |gradeid1 |gradeid2 |gradeid3 3|etc...
01/01/01|estValue1|estValue2||estValue3|etc....
01/01/02|estValue1|estValue2||estValue3|etc....
我有一個查詢,可以選擇一個記錄,但我需要他們全部按日期排序:
select eh.id, eh.date as wdate,
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '1') as '1',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '2') as '2',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '3') as '3',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '4') as '4',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '5') as '5',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '6') as '6',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '7') as '7',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '8') as '8',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '9') as '9',
(select estValue from nas_estimatehistory where `date` like '2011-03-%' and gradeid = '10') as '10'
from nas_estimatehistory eh
group by wdate
order by `wdate` asc
limit 1;
返回差不多就是我所需要的,但只有1行,如果我刪除了極限,然後我得到每行[12行]的一行,但所有的列值是相同的[他們應該都是不同的],即estValue在每行和列應該是一個唯一的值...
我不確定最好的方法是什麼。
-Thanks -Sean
表格模式(創建語句)和樣例數據(表示爲SQL INSERT語句)在這類問題中非常有用。 – outis 2011-03-26 18:12:41