mysql view or query to show data from different dates?
I have a mySQL view that gives me totals from other tables. Something like this: Table 1:
ID | Parameter1 | Parameter2 | ValidFrom | ValidUntil
Table 2:
ID | Parameter3 | Parameter4 | ValidFrom | ValidUntil
The view:
SumOfParameter1 | SumOfParameter2 | SumOfParameter3 | SumOfParameter4
The thing is I want to implement a way to have data in the view corespondent to the periods of time that the data is valid. For example if a row in Table1 is valid from 01-2011 to 03-2011 and a row in Table2 is valid from 01-2011 to 02-2011 and another row from 02-2011 to 03-2001 my view would look like this:
SumOfParameter1 | SumOfParameter2 | SumOfParameter3 | SumOfParameter4 | 01-2011
SumOfParameter1 | SumOfParameter2 | SumOfParameter3 | SumOfParameter4 | 02-2011
SumOfParameter1 | SumOfParameter2 | SumOfParameter3 | SumOfParameter4 | 03-2011
I know I can do this using a query in PHP and specifying the date I want, but is it possible to do it in a view in the 开发者_开发技巧way I mentioned above?
Try:
SELECT Parameter1, Parameter2, Parameter3, Parameter4
FROM table1, table2
ORDER BY ValidUntil
GROUP BY ValidUntil
精彩评论