SQL也疯狂:MySQL绘制简单几何图形(2013-04-12)
用SQL SELECT 语句而非存储过程来画基本几何图形,以加深对 SELECT 语句的理解。因为 SELECT 语句隐含了一个或多个循环,看上去只是一个语句,其实包含了一个完整的程序所包含的顺序、分支、循环,因此不必用存储过程就能实现一般的程序的功能 需要有一个表
- 用SQL SELECT 语句而非存储过程来画基本几何图形,以加深对 SELECT 语句的理解。因为 SELECT 语句隐含了一个或多个循环,看上去只是一个语句,其实包含了一个完整的程序所包含的顺序、分支、循环,因此不必用存储过程就能实现一般的程序的功能
- 需要有一个表有 100 行以上的数据,数据是什么没关系,因为下面的语句已经避开使用数据库记录中的数据了。如果没有现成的就使用如下代码生成一个吧:
语句
DELIMITER ;; DROP PROCEDURE IF EXISTS test_num;; CREATE PROCEDURE test_num(MAX_COUNT INTEGER) BEGIN DECLARE i INTEGER; SET i = 0; DROP TABLE IF EXISTS number; CREATE TABLE number(num INT(10), PRIMARY KEY (num)); WHILE i < MAX_COUNT DO INSERT INTO number(num) VALUES (i); SET i = i + 1; END WHILE; END;; DELIMITER ; CALL test_num(100);
Copy after login
- 画横线
语句
结果
SET @w := 24; # 宽 <span>SELECT</span> repeat(<span>"*"</span>, @w) AS line;
Copy after login+--------------------------+ | line | +--------------------------+ | ************************ | +--------------------------+
Copy after login - 画竖线
语句
结果
SET @h := 12; # 高 SET @y := 0; # y 座标 <span>SELECT</span> <span>"*"</span> AS line <span>FROM</span> number <span>WHERE</span> @y < @h and @y := @y + 1;
Copy after login+------+ | line | +------+ | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | +------+
Copy after login - 画斜线
语句
结果
SET @h := 12; # 高 SET @y := 0; # y 座标 SET @rate := 2; # x y 座标比例(斜率倒数) <span>SELECT</span> lpad(<span>"*"</span>, @y * @rate, <span>" "</span>) AS line <span>FROM</span> number <span>WHERE</span> @y < @h and @y := @y + 1;
Copy after login+--------------------------+ | line | +--------------------------+ | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | +--------------------------+
Copy after login - 画XX
语句
SET @h := 12; # 高 SET @w := 24; # 宽 SET @y := 0; # y 座标 SET @rate := @w / @h; # x y 座标比例(斜率倒数) <span> SELECT</span> X <span>FROM</span> ( <span> SELECT</span> concat(lpad(<span>"*"</span>, @y * @rate - 1, <span>" "</span>), repeat(<span>" "</span>, @w - 2 * @y * @rate - 2 + 1), IF(@w - 2 * @y * @rate - 2 + 1 <= 0, <span>"", "</span>*")) AS X <span>FROM</span> number <span>WHERE</span> @y < @h / 2 and @y := @y + 1 UNION ALL <span> SELECT</span> concat(lpad(<span>"*"</span>, @y * @rate - 1, <span>" "</span>), repeat(<span>" "</span>, @w - 2 * @y * @rate - 2 + 1), IF(@w - 2 * @y * @rate - 2 + 1 <= 0, <span>"", "</span>*")) AS X <span>FROM</span> number <span>WHERE</span> @y < @h and (@y := @y - 1) > 0 ) AS Temp;
Copy after login -
结果
+-----------------------+ | X | +-----------------------+ | * * | | * * | | * * | | * * | | * * | | * | | * * | | * * | | * * | | * * | | * * | +-----------------------+
Copy after login - 画矩形
语句
SET @x := 0; # x 座标 SET @rate := 2; # x y 轴长比例,调整显示效果 SET @h := 12; # 长 SET @w := 12; # 宽 <span> SELECT</span> rect <span>FROM</span> ( <span>SELECT</span> repeat(<span>"*"</span>, @w * @rate) AS rect UNION ALL <span> SELECT</span> concat(<span>"*"</span>, repeat(<span>" "</span>, @w * @rate - 2), <span>"*"</span>) <span>FROM</span> number <span>WHERE</span> @x < @h and @x := @x + 1 UNION ALL <span>SELECT</span> repeat(<span>"*"</span>, @w * @rate) ) AS Temp;
Copy after login -
-
结果
+--------------------------+ | rect | +--------------------------+ | ************************ | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | ************************ | +--------------------------+
Copy after login - 把XX用矩形框起来
语句
SET @h := 12; # 高 SET @w := 24; # 宽 SET @y := 0; # y 座标 SET @rate := @w / @h; # x y 座标比例(斜率倒数) <span> SELECT</span> rectXX <span>FROM</span> ( <span>SELECT</span> repeat(<span>"*"</span>, (@w - 2 * 2)) AS rectXX UNION ALL <span>SELECT</span> concat(<span>"*"</span>, lpad(<span>"*"</span>, @y * @rate - 1, <span>" "</span>), repeat(<span>" "</span>, (@w - 2 * 2) - 2 * @y * @rate - 2 + 1), IF((@w - 2 * 2) - 2 * @y * @rate - 2 + 1 <= 0, <span>"", "</span>*<span>"), repeat("</span> <span>", @y * @rate - 1), "</span>*") <span>FROM</span> number <span>WHERE</span> @y < (@h - 2) / 2 and @y := @y + 1 UNION ALL <span> SELECT</span> concat(<span>"*"</span>, lpad(<span>"*"</span>, @y * @rate - 1, <span>" "</span>), repeat(<span>" "</span>, (@w - 2 * 2) - 2 * @y * @rate - 2 + 1), IF((@w - 2 * 2) - 2 * @y * @rate - 2 + 1 <= 0, <span>"", "</span>*<span>"), repeat("</span> <span>", @y * @rate - 1), "</span>*") <span>FROM</span> number <span>WHERE</span> @y < (@h - 2) and (@y := @y - 1) > 0 UNION ALL <span>SELECT</span> repeat(<span>"*"</span>, (@w - 2 * 2)) AS rect ) AS Temp;
Copy after login -
结果
+----------------------+ | rectXX | +----------------------+ | ******************** | | ** * * | | * * * * | | * * * * | | * * * * | | * * * | | * * * * | | * * * * | | * * * * | | ** * * | | ******************** | +----------------------+
Copy after login - 画等腰三角形
语句
结果
SET @h := 10; # 高 SET @w := 10; # 底 SET @x := 0; # x 座标 SET @y := 0; # y 座标 SET @k := @w/@h/2; # 1/2底高比例,即边的斜率的倒数 SET @rate := 2; # x y 轴比例,调整显示效果 <span>SELECT</span> concat(repeat(<span>" "</span>, @rate * (@w / 2 - (@y - 1) * @k) - 1), <span>"*"</span>, repeat(IF(@y = @h, <span>"*"</span>, <span>" "</span>), @rate * (2 * (@y - 1) * @k) - 1), IF(@y = 1, <span>"", "</span>*")) AS triangle <span>FROM</span> number <span> WHERE</span> (@y := @y + 1) AND @y <= @h;
Copy after login+---------------------+ | triangle | +---------------------+ | * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | ******************* | +---------------------+
Copy after login - 画正弦曲线
语句
结果
SET @x := 0; # x 座标 SET @offset := 15; # y 偏移 SET @am := 15; # 振幅 SET @rate := 10; # x y 轴比例(影响波长),调整显示效果 SET @len := 30; # 长度 <span> SELECT</span> lpad(<span>"*"</span>, round(@am * sin(@x * 3.14 / @rate) + @offset) + 1, ' ') AS 'sin' <span>FROM</span> number <span>WHERE</span> (@x := @x + 1) < @len;
Copy after login+---------------------------------+ | sin | +---------------------------------+ | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | | * | +---------------------------------+
Copy after login - 画圆
语句
结果
SET @r := 12; # 半径 SET @d := 2 * (@r + 1); # 图形范围,即直径(避免减到0,偏移 1) SET @x := 0; # x 座标 SET @y := @r + 1; # y 座标 SET @rate := 2; # x y 轴长比例,调整显示效果 <span> SELECT</span> circle <span>FROM</span> ( <span>SELECT</span> @x := round(@rate * sqrt(pow(@r, 2) - pow(@y, 2))) + 1, concat(lpad(<span>"*"</span>, @d - @x, <span>" "</span>), lpad(<span>"*"</span>, 2 * @x, <span>" "</span>)) AS circle <span>FROM</span> number <span>WHERE</span> (@y := @y - 1) > 0 AND @y <= @r UNION ALL <span>SELECT</span> @x := round(@rate * sqrt(pow(@r, 2) - pow(@y, 2))) + 1, concat(lpad(<span>"*"</span>, @d - @x, <span>" "</span>), lpad(<span>"*"</span>, 2 * @x, <span>" "</span>)) AS circle <span>FROM</span> number <span>WHERE</span> (@y := @y + 1) > 0 AND @y <= @r ) AS Temp;
Copy after login+-----------------------------------------------------+ | circle | +-----------------------------------------------------+ | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | | * * | +-----------------------------------------------------+
Copy after login
- UNION 后再用一个 SELECT 仅是包装一下,看起来像一个整体,不那么松散
- 回头想想,上面用了 UNION 的都可以用 IF 来整合在一个语句里,不过会显得很臃肿
- 亚丹
seesea2517#gmail#com
http://seesea.blog.chinaunix.net
http://blog.csdn.net/nicenight
http://my.oschina.net/seesea2517
x

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
