Home Database Mysql Tutorial SQL也疯狂:MySQL绘制简单几何图形(2013-04-12)

SQL也疯狂:MySQL绘制简单几何图形(2013-04-12)

Jun 07, 2016 pm 02:57 PM
mysql sql Geometry Simple draw

用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
  1. 画横线 语句
    SET @w := 24;  # 宽 <span>SELECT</span> repeat(<span>"*"</span>, @w) AS line;
    
    Copy after login
    结果
    +--------------------------+
    | line                     |
    +--------------------------+
    | ************************ |
    +--------------------------+
    
    Copy after login
  2. 画竖线 语句
    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
  3. 画斜线 语句
    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
  4. 画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
  5. 结果
    +-----------------------+
    | X                     |
    +-----------------------+
    | *                   * |
    |   *               *   |
    |     *           *     |
    |       *       *       |
    |         *   *         |
    |           *           |
    |         *   *         |
    |       *       *       |
    |     *           *     |
    |   *               *   |
    | *                   * |
    +-----------------------+
    
    Copy after login
  6. 画矩形 语句
    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

  7. 结果
    +--------------------------+
    | rect                     |
    +--------------------------+
    | ************************ |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | *                      * |
    | ************************ |
    +--------------------------+
    
    Copy after login
  8. 把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
  9. 结果
    +----------------------+
    | rectXX               |
    +----------------------+
    | ******************** |
    | **               * * |
    | *  *           *   * |
    | *    *       *     * |
    | *      *   *       * |
    | *        *         * |
    | *      *   *       * |
    | *    *       *     * |
    | *  *           *   * |
    | **               * * |
    | ******************** |
    +----------------------+
    
    Copy after login
  10. 画等腰三角形 语句
    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
  11. 画正弦曲线 语句
    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
  12. 画圆 语句
    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
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to use AWS Glue crawler with Amazon Athena How to use AWS Glue crawler with Amazon Athena Apr 09, 2025 pm 03:09 PM

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

See all articles