Insert Into Table Using Select Statement

Someday, I need to insert some records into table from a query or select statement. The basic syntax:

INSERT INTO  (xxxfield, yyyfield, zzzfield)
SELECT xxx, yyy, zzz from abc 

For example, if I want to inserting rows into table member from viewuser table

INSERT INTO member(name,username,lastlogin)
SELECT uname,uusername,ulastlogin
from viewuser
where state='CA';

if you want to add constant value, just add it on the query

INSERT INTO member(name,username,password,lastlogin)
SELECT uname,uusername,'bOH4y',ulastlogin
from viewuser
where state='CA';

Hope this simple article can help 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s