简体   繁体   中英

How to select data of one table and insert in other table?

I have a project in java where i want that certain data from one table (that is in Sql management studio) is selected and that is inserted in other table. So that i can access data on a jsp page from second table. How to do this?

One method would be to iterate through the table while writing the values into an array. Once the data has been stored into the array you can re-iterate through the array but this time inserting the values into a new table.

This may not be the most efficient method, I am sure someone else will chime in if so.

Another method which does not require Java would be to use the Select As statement in SQL, see example.

CREATE TABLE suppliers
  AS (SELECT *
         FROM companies
         WHERE id > 1000);

Or if you already have a table created you can do the following,

INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE city = 'Newark';

If you use SQL, you can use SELECT INTO statements to achieve this easily:

SELECT Column1,Column2
INTO SecondTable
FROM FirstTable
WHERE Column3='Whatever'

This will copy the data from FirstTable into SecondTable .

See This Link for more examples

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