简体   繁体   中英

How can I alter a temp table?

I need to create a temp table, than add a new int NOT NULL AUTO_INCREMENT field to it so I can use the new field as a row number. Whats wrong with my query?

SELECT post, newid FROM ((SELECT post`test_posts`) temp
ALTER TABLE temp ADD COLUMN newid int NOT NULL AUTO_INCREMENT)

edit:

SELECT post, newid FROM ((SELECT post, newid as int NOT NULL AUTO_INCREMENT FROM `test_posts`) temp

This didn't work ether.

Not that this means it's impossible, but I haven't seen any SQL version that will allow you to modify a table from within a SELECT. Pull the alter table out and make it a separate statement. After you fix any syntax issues, you should be good.

Also, it doesn't look like you actually have a "temp table" to alter. Rather, you're looking for a solution that will let you add an arbitrary id to the result of your "SELECT post..." query. I don't know which engine you're using, but a sequence counter, rowid, rownum, or other similar feature would better fit your needs.

If you need a row number and don't want to actually create a temporary table, you can achieve it by using user variables.

SET @my_row_num =0;
SELECT @my_row_num := @my_row_num+1 as row_number, post, newid FROM ((SELECT post`test_posts`) temp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM