sql 数据库
有一张表 :id 时间 地区 价格
1 2001 上海 5
2 2001 上海 5
2 2001 山东 6
3 2002 上海 8
4 2002 山东 9
7 2003 山东 10
8 2003 上海 空
想将它扫描出来放在前台页面成这样
时间 上海 山东
2001 5 6
2002 8 9
2003 10
回复讨论(解决方案)
这个用到存储过程的,不然应该实现不了,除非你知道有几个地区,比如你就上海跟山东这2个地区,那可以用一句弄出来!
看看其他大神有没有好的方法!
CREATE TABLE `mytable` ( id int, 时间 varchar(4), 地区 varchar(50), 价格 decimal);insert into mytable select 1,'2001','上海', 5union allselect 2,'2001','上海', 5 union allselect 2,'2001','山东', 6union allselect 3,'2002','上海', 8 union allselect 4,'2002','山东', 9union allselect 7,'2003','山东', 10union allselect 8,'2003','上海', null;select * from mytable;select 时间,( case 地区 when '上海' then 价格 else null end) as '上海',( case 地区 when '山东' then 价格 else null end) as '山东'from mytablegroup by 时间;
$rs = mysql_query('select * from tbl_name');while($r = mysql_fetch_assoc($rs)) { $res[$r['时间']][$r['地区']] = $r['价格'];}$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";
上海 山东2001 5 6 2002 8 9 2003 10
CREATE TABLE `mytable` ( id int, 时间 varchar(4), 地区 varchar(50), 价格 decimal);insert into mytable select 1,'2001','上海', 5union allselect 2,'2001','上海', 5 union allselect 2,'2001','山东', 6union allselect 3,'2002','上海', 8 union allselect 4,'2002','山东', 9union allselect 7,'2003','山东', 10union allselect 8,'2003','上海', null;select * from mytable;select 时间,( case 地区 when '上海' then 价格 else null end) as '上海',( case 地区 when '山东' then 价格 else null end) as '山东'from mytablegroup by 时间;
这个就是假设地区是已知的(只要山东与上海2个地区),不具备通用性!
参考这个帖子第4种方法 http://www.cnblogs.com/lhj588/archive/2012/06/15/2550392.html
楼上的方法都不错,学习了

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft