auto column

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmk128@hotmail.com

    #1

    auto column

    Hi
    I want to select a single row called "row1" but let mysql auto
    append a NON-exist column call "row2" of it. And i want that non-exist
    column be auto-increment. how to?


    If i "select row1 from mytable", then i get

    -----------
    - row1 -
    -----------
    - a -
    - b -
    - c -
    - d -
    -----------

    but i want:
    ----------------------
    - row1 -- row1 -
    ----------------------
    - a - 1 -
    - b - 2 -
    - c - 3 -
    - d - 4 -
    ----------------------

    thanks
    from Peter (cmk128@hotmail .com)

  • Chander Ganesan

    #2
    Re: auto column

    set @counter=0;
    select row1, @counter:=@coun ter+1 as row2 from table;

    The first statement intializes a variable to 0, the SQL statement
    increments the counter with each row that you retrieve.

    --
    Chander Ganesan
    Open Technology Group, Inc.
    Phone: 877-258-8987/919-463-0999

    MySQL training from the experts.

    Comment

    • vin.dor@gmail.com

      #3
      Re: auto column

      Hi Chander,

      I tried to do something similar in SQL Server 7.0:

      declare @counter int
      select @counter=0
      select @counter=@count er+1, field1 from table

      However, I got the following error:
      A SELECT statement that assigns a value to a variable must not be
      combined with data-retrieval operations.

      Is it possible to use the similar statement you provided in MS SQL
      Server?

      Thanks,
      Vincent


      Chander Ganesan wrote:[color=blue]
      > set @counter=0;
      > select row1, @counter:=@coun ter+1 as row2 from table;
      >
      > The first statement intializes a variable to 0, the SQL statement
      > increments the counter with each row that you retrieve.
      >
      > --
      > Chander Ganesan
      > Open Technology Group, Inc.
      > Phone: 877-258-8987/919-463-0999
      > http://www.otg-nc.com
      > MySQL training from the experts.[/color]

      Comment

      • Chander Ganesan

        #4
        Re: auto column

        There might be an equivalent for MS SQL, but I don't know it offhand.
        I spend most of my time working with PostgreSQL, MySQL and occasionally
        Oracle. I assumed you were using MySQL (since this is
        mailing.databas e.mysql ).

        --
        Chander Ganesan
        Open Technology Group, Inc.
        Phone: 877-258-8987/919-463-0999

        MySQL training from the experts.

        Comment

        Working...