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

      <legend id='DYyDa'><style id='DYyDa'><dir id='DYyDa'><q id='DYyDa'></q></dir></style></legend>
    1. <small id='DYyDa'></small><noframes id='DYyDa'>

      <tfoot id='DYyDa'></tfoot>
    2. 创建一个带有 num_rows 列的视图 - MySQL

      Create a view with column num_rows - MySQL(创建一个带有 num_rows 列的视图 - MySQL)

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

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

              • <small id='P2HeX'></small><noframes id='P2HeX'>

                本文介绍了创建一个带有 num_rows 列的视图 - MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要创建一个视图,它有一个名为 row_num 的列,将在其中插入行号,就像普通表中的自动增量一样.

                I need to create a view that has a column named row_num where it will be inserted the row number, just like an auto increment in a normal table.

                假设我有一张普通的桌子:

                Let's say I've this normal table:

                | country | name | age | price |
                --------------------------------
                | US      | john | 22  | 20    |
                | France  | Anne | 10  | 15    |
                | Sweden  | Alex | 49  | 10    |
                

                等等……

                我要创建的视图是:

                    | country | name | price | row_num |
                    ------------------------------------
                    | US      | john |  20   |    1    |
                    | France  | Anne |  10   |    2    |
                    | Sweden  | Alex |  5    |    3    |
                

                等等……

                我可以通过一次选择生成 row_num:

                I can generate the row_num with a single select:

                SELECT @i:=@i+1 AS row_num, testing.country, testing.name, testing.price
                FROM testing testing,(SELECT @i:=0) derivedTable
                order by name
                

                但我的问题是将上面的查询与创建视图的查询结合起来.这是我正在尝试的组合查询:

                But my problem is to combine the query above with the query creating the view. This is the combined query I'm trying:

                CREATE OR REPLACE view vwx (country, name, price, num_row) AS SELECT mytable.country, mytable.name, mytable.price, @i:=@i+1 AS row_number
                    FROM testing testing,(SELECT @i:=0) derivedTable
                    order by name;
                

                我收到以下错误:#1351 - 视图的 SELECT 包含变量或参数

                I get the following error: #1351 - View's SELECT contains a variable or parameter

                我知道我不能在带有视图的选择中使用选择,但我没有看到以我想要的方式执行此视图的其他方法,但我确定有一种方法可以做到这一点,但我只是不这样做不知道怎么办可能有函数或过程,但我对它们真的很陌生,所以我不习惯在 mysql 中创建函数或过程.

                I know I can't use a select inside a select with views but I don't see other way to do this view the way I want, but I'm sure there is a way to do this but I just don't know how. Probably with functions or procedures but I'm really new to them so I'm not comfortable with creating functions or procedures in mysql.

                我希望我说清楚了,否则我很乐意更详细地解释自己.

                I hope I made myself clear otherwise I'm more than happy to explain myself in further detail.

                推荐答案

                我找到了解决方案:

                首先创建一个函数:

                delimiter //
                
                CREATE FUNCTION `func_inc_var_session`() RETURNS int
                    NO SQL
                    NOT DETERMINISTIC
                     begin
                      SET @var := @var + 1;
                      return @var;
                     end
                     //
                
                delimiter ;
                

                然后将@var 设置为您想要开始的数字.在这种情况下为零.

                Then set @var to the number you want to start with. In this case zero.

                设置@var=0;

                然后创建如下视图:

                CREATE OR REPLACE VIEW myview (place, name, hour, price, counter) 
                AS SELECT place, name, hour, price, func_inc_var_session() 
                FROM yourtable
                WHERE input_conditions_here;
                

                这里的技巧是您可能会在计数器列上看到 NULL.如果发生这种情况,请再次将 @var 设置为您的号码,然后再次执行 SELECT *,您将看到计数器列正确填充.

                The trick here is that you may see NULL on the counter column. If this happens please set @var again to your number and then do the SELECT * again and you'll see the counter column properly populated.

                这篇关于创建一个带有 num_rows 列的视图 - MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)
                MySQL cumulative sum grouped by date(按日期分组的 MySQL 累计总和)
              • <tfoot id='1vEqJ'></tfoot>

                  <small id='1vEqJ'></small><noframes id='1vEqJ'>

                  • <bdo id='1vEqJ'></bdo><ul id='1vEqJ'></ul>

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

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