Using SELECT to INSERT records in SQL

In MS SQL, very often I want to insert records into a table based on the record set of another query I have.

I always end up looking up this over and over, so here it is for your reference and mine. 

INSERT table_desitination 
(td_field1, td_field2) SELECT ts_field1, ts_field2 FROM table_source

If you put a :3 in my ask...

…I’ll give you numbers for blog ratings happy fun time. And by that I mean, I’ll rate your urls and themes and blog and all that on a scale of 1-10.

Yay.

I am so bored. Seriously. Just do it. Please.

Select and Insert or Update in MySQL

I forget this every time.

1. Select and Insert

INSERT INTO
	table1 ( table1.column1, table1.column2 )
SELECT
	table2.code, table2.name
FROM
	table2;

2. Select and Update (actually, JOIN UPDATE)

UPDATE
	table1, table2
SET
    table1.column1 = table2.column1
WHERE
	table1.column_key = table2.column_key;

Reference:

http://entireboy.egloos.com/4657692

Bezig met laden van meer berichten…