Shafizadeh February 2016

How to insert the number of id in the neighbor column?

I have a table with this structure:

+----+-------------------+
| id | identical_with_id |
+----+-------------------+

id is auto-increment and I need to insert in the this column identical_with_id exactly identical value with id's value. How can I do that?

I want something like this:

+----+-------------------+
| id | identical_with_id |
+----+-------------------+
| 1  | 1                 |
| 2  | 2                 |
| 3  | 3                 |
+----+-------------------+

Note, in reality the table is also containing some other values for identical_with_id column, like this:

+----+-------------------+
| id | identical_with_id |
+----+-------------------+
| 1  | 1                 |    -- it is a question
| 2  | 1                 |    -- it is the answer of the above question
| 3  | 1                 |    -- it is the answer of the above question
| 4  | 4                 |    -- it is a question
| 5  | 4                 |    -- it is the answer of the above question
| 6  | 6                 |    -- it is a question
| 7  | 7                 |    -- it is a question
| 8  | 7                 |    -- it is the answer of the above question
| 9  | 7                 |    -- it is the answer of the above question
+----+-------------------+

Now I need to insert a question in the table. And I want to know how can I fill this column: identical_with_id ?

Answers


Nijraj Gelani February 2016

No need to update the table. You can do it in the single insert query like shown below.

INSERT INTO `table_name` VALUES(NULL, (SELECT `AUTO_INCREMENT` FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name'));

Although I don't see the point of doing so...

Post Status

Asked in February 2016
Viewed 2,631 times
Voted 10
Answered 1 times

Search




Leave an answer