Need SQL command that will insert a row after specific row

*爱你&永不变心* 提交于 2020-01-14 06:19:05

问题


I need SQL command that will insert a row after specific row. Example:-

Before table

Id.         Name.      
1.           Xyz.          
2.           Xyz
3.           Xyz    

Want result need to add 'Abc' data after each 'xyz' having same id like:-

Id.         Name.      
1.           Xyz.    
1.           Abc      
2.           Xyz
2.           Abc
3.           Xyz
3.           Abc

回答1:


You need UNION ALL:

SELECT t.*
FROM (SELECT id, Name
      FROM table t
      UNION ALL
      SELECT ID, 'Abc'
      FROM table t
    ) t
ORDER BY ID, NAME;

This will not insert row, it just provide you run time view. If you want only insert then you need to truncate your table (note : take a backup of current table ) & perform insert operation.




回答2:


INSERT INTO your_table 
SELECT Id
       ,'Abc' AS Name
FROM your_table
Where Name = 'Xyz'

I am not sure if OVERRIDING SYSTEM VALUE is needed, it depends on your table. But this query should do what you need. It will get all the rows that contain the Xyz value and use the same id and a different name. Then if you order by Id because remember ordering is never guaranteed if you don't use order by




回答3:


Try this

START TRANSACTION;

UPDATE table_sample SET column = column+1 WHERE column >= some_value_here ";

INSERT INTO table_sample (id, column_name) VALUES
(NULL, 'value_here');

COMMIT;


来源:https://stackoverflow.com/questions/55122499/need-sql-command-that-will-insert-a-row-after-specific-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!