Ask for database help, the big God can solve SQL database
There is a table:
Price of id Time region
1 2001 Shanghai 5
2 2001 Shanghai 5
2 2001 Shandong 6
3 2002 Shanghai 8
4 2002 Shandong 9
7 2003 Shandong 10
8 2003 Shanghai Kong
I want to scan it and put it on the front-end page.
Time Shanghai Shandong
2001 5 6
2002 8 9
2003 10
Reply to discussion (solution)
This is a stored procedure. it cannot be implemented unless you know several regions, such as Shanghai and Shandong!
Check if other great gods have any good methods!
Create table 'mytable' (id int, time varchar (4), region varchar (50), price decimal); insert into mytable select 1, '123', 'Shanghai ', 5 union allselect 2, '123', 'Shanghai', 5 union allselect 2, '123', 'Shandong ', 6 union allselect 3, '123', 'Shanghai ', 8 union allselect 4, '123', 'Shandong ', 9 union allselect 7, '123', 'Shandong', 10 union allselect 8, '123', 'Shanghai ', null; select * from mytable; select time, (case region when 'Shanghai' then price else null end) as 'Shanghai ', (case when 'Shandong 'Then price else null end) as 'Shandong 'from mytablegroup by time;
$ Rs = mysql_query ('select * from tbl_name '); while ($ r = mysql_fetch_assoc ($ rs )) {$ res [$ r ['Time'] [$ r ['region'] = $ r ['price'];} $ t = array_keys (current ($ res); echo "\ t ". join ("\ t", $ t ). "\ n"; foreach ($ res as $ k => $ r) {echo $ k; foreach ($ t as $ v) echo "\ t {$ r [$ v]}"; echo "\ n ";
Shanghai Shandong 20015 6 20028 9 2003 10
Create table 'mytable' (id int, time varchar (4), region varchar (50), price decimal); insert into mytable select 1, '123', 'Shanghai ', 5 union allselect 2, '123', 'Shanghai', 5 union allselect 2, '123', 'Shandong ', 6 union allselect 3, '123', 'Shanghai ', 8 union allselect 4, '123', 'Shandong ', 9 union allselect 7, '123', 'Shandong', 10 union allselect 8, '123', 'Shanghai ', null; select * from mytable; select time, (case region when 'Shanghai' then price else null end) as 'Shanghai ', (case when 'Shandong 'Then price else null end) as 'Shandong 'from mytablegroup by time;
This is assumed that the region is known (as long as there are two regions in Shandong and Shanghai) and is not universal!
Refer to this post 4th methods http://www.cnblogs.com/lhj588/archive/2012/06/15/2550392.html
The upstairs method is good. I learned it.