• <bdo id='ozYD5'></bdo><ul id='ozYD5'></ul>

    <small id='ozYD5'></small><noframes id='ozYD5'>

      <legend id='ozYD5'><style id='ozYD5'><dir id='ozYD5'><q id='ozYD5'></q></dir></style></legend>

    1. <tfoot id='ozYD5'></tfoot>
      <i id='ozYD5'><tr id='ozYD5'><dt id='ozYD5'><q id='ozYD5'><span id='ozYD5'><b id='ozYD5'><form id='ozYD5'><ins id='ozYD5'></ins><ul id='ozYD5'></ul><sub id='ozYD5'></sub></form><legend id='ozYD5'></legend><bdo id='ozYD5'><pre id='ozYD5'><center id='ozYD5'></center></pre></bdo></b><th id='ozYD5'></th></span></q></dt></tr></i><div id='ozYD5'><tfoot id='ozYD5'></tfoot><dl id='ozYD5'><fieldset id='ozYD5'></fieldset></dl></div>

      SQL SELECT 获取前 N 个正整数

      SQL SELECT to get the first N positive integers(SQL SELECT 获取前 N 个正整数)

          <bdo id='VUYTc'></bdo><ul id='VUYTc'></ul>
        • <legend id='VUYTc'><style id='VUYTc'><dir id='VUYTc'><q id='VUYTc'></q></dir></style></legend>
            <tbody id='VUYTc'></tbody>

                <tfoot id='VUYTc'></tfoot>
              1. <i id='VUYTc'><tr id='VUYTc'><dt id='VUYTc'><q id='VUYTc'><span id='VUYTc'><b id='VUYTc'><form id='VUYTc'><ins id='VUYTc'></ins><ul id='VUYTc'></ul><sub id='VUYTc'></sub></form><legend id='VUYTc'></legend><bdo id='VUYTc'><pre id='VUYTc'><center id='VUYTc'></center></pre></bdo></b><th id='VUYTc'></th></span></q></dt></tr></i><div id='VUYTc'><tfoot id='VUYTc'></tfoot><dl id='VUYTc'><fieldset id='VUYTc'></fieldset></dl></div>

                <small id='VUYTc'></small><noframes id='VUYTc'>

                本文介绍了SQL SELECT 获取前 N 个正整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要得到一个包含前 N 个正整数的结果集.是否可以仅使用标准 SQL SELECT 语句来获取它们(不提供任何计数表)?

                I need to get a result set containing the first N positive integers. Is it possible to use only standard SQL SELECT statement to get them (without any count table provided)?

                如果不可能,是否有任何特定的 MySQL 方法可以实现这一目标?

                If it's not possible, is there any specific MySQL way to achieve this?

                推荐答案

                似乎你想要的是一个 dummy rowset.

                Seems that what you want is a dummy rowset.

                MySQL中,没有表是不可能的.

                In MySQL, it's impossible without having a table.

                大多数主要系统都提供了一种方法:

                Most major systems provide a way to do it:

                • Oracle中:

                SELECT  level
                FROM    dual
                CONNECT BY
                        level <= 10
                

              2. SQL Server中:

                WITH    q AS
                        (
                        SELECT  1 AS num
                        UNION ALL
                        SELECT  num + 1
                        FROM    q
                        WHERE   num < 10
                        )
                SELECT  *
                FROM    q
                

              3. PostgreSQL中:

                SELECT  num
                FROM    generate_series(1, 10) num
                

              4. MySQL 缺少这样的东西,这是一个严重的缺点.

                MySQL lacks something like this and this is a serious drawback.

                我写了一个简单的脚本来为我博客文章中的示例表生成测试数据,也许会有用:

                I wrote a simple script to generate test data for the sample tables in my blog posts, maybe it will be of use:

                CREATE TABLE filler (
                        id INT NOT NULL PRIMARY KEY AUTO_INCREMENT
                ) ENGINE=Memory;
                
                CREATE PROCEDURE prc_filler(cnt INT)
                BEGIN
                        DECLARE _cnt INT;
                        SET _cnt = 1;
                        WHILE _cnt <= cnt DO
                                INSERT
                                INTO    filler
                                SELECT  _cnt;
                                SET _cnt = _cnt + 1;
                        END WHILE;
                END
                $$
                

                你调用这个过程,表格就会填满数字.

                You call the procedure and the table gets filled with the numbers.

                您可以在会话期间重复使用它.

                You can reuse it during the duration of the session.

                这篇关于SQL SELECT 获取前 N 个正整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Bogus foreign key constraint fail(虚假外键约束失败)
                how to get last insert id after insert query in codeigniter active record(如何在codeigniter活动记录中插入查询后获取最后一个插入ID)
                Force InnoDB to recheck foreign keys on a table/tables?(强制 InnoDB 重新检查表/表上的外键?)
                How to auto generate migrations with Sequelize CLI from Sequelize models?(如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?)
                Clear MySQL query cache without restarting server(无需重启服务器即可清除 MySQL 查询缓存)
                ALTER TABLE to add a composite primary key(ALTER TABLE 添加复合主键)

                    <i id='jEitn'><tr id='jEitn'><dt id='jEitn'><q id='jEitn'><span id='jEitn'><b id='jEitn'><form id='jEitn'><ins id='jEitn'></ins><ul id='jEitn'></ul><sub id='jEitn'></sub></form><legend id='jEitn'></legend><bdo id='jEitn'><pre id='jEitn'><center id='jEitn'></center></pre></bdo></b><th id='jEitn'></th></span></q></dt></tr></i><div id='jEitn'><tfoot id='jEitn'></tfoot><dl id='jEitn'><fieldset id='jEitn'></fieldset></dl></div>
                        <bdo id='jEitn'></bdo><ul id='jEitn'></ul>
                      • <tfoot id='jEitn'></tfoot>

                          <small id='jEitn'></small><noframes id='jEitn'>

                        1. <legend id='jEitn'><style id='jEitn'><dir id='jEitn'><q id='jEitn'></q></dir></style></legend>

                            <tbody id='jEitn'></tbody>