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 🙂