Computing.Net > Forums > Database > Numerated inserts into a mysql tabl

Numerated inserts into a mysql tabl

Reply to Message Icon

Original Message
Name: jplascencia
Date: November 21, 2007 at 07:41:06 Pacific
Subject: Numerated inserts into a mysql tabl
OS: LAMP
CPU/Ram: LAMP
Model/Manufacturer: LAMP
Comment:

Hi,

I want to use a populate a table using a
INSERT ... [INTO] tbl_name [(col_name,...)]
SELECT ...
structure.

The thing here is that I want to have a order column, for example

register 4, 1, 234
register 45, 2, 834
register 54, 3, 5326
register 87, 4, 8753

the 2nd column (1, 2, 3, 4) has that order that I need and it´s not in the source table.

Any suggestions to do it?

Thanks.


Report Offensive Message For Removal

Response Number 1
Name: mdow
Date: December 10, 2007 at 12:39:48 Pacific
Subject: Numerated inserts into a mysql tabl
Reply: (edit)

One way to do this would be create an alias of your table, join the table to itself, and then add in a running count of matching records.

SELECT (SELECT COUNT(*) FROM myTable WHERE myTable.uniqueID < mt2.uniqueID)+1 AS INDX
FROM myTable INNER JOIN myTable AS mt2 ON myTable.uniqueID = mt2.uniqueID;

Note that the +1 is used to start the count with 1. You need to omit that if you want to start the count at 0.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Numerated inserts into a mysql tabl

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software