Ask for database help, the big God can solve

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.