Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Select Into Field

I want to accomplish something of the following:

Select DISTINCT(tableA.column) INTO tableB.column FROM tableA

The goal would be to select a distinct data set and then insert that data into a specific column of a new table.

like image 770
somejkuser Avatar asked Dec 10 '22 18:12

somejkuser


2 Answers

SELECT column INTO tableB FROM tableA

SELECT INTO will create a table as it inserts new records into it. If that is not what you want (if tableB already exists), then you will need to do something like this:

INSERT INTO tableB (
column
)
SELECT DISTINCT
column
FROM tableA

Remember that if tableb has more columns that just the one, you will need to list the columns you will be inserted into (like I have done in my example).

like image 155
Gabriel McAdams Avatar answered Dec 24 '22 18:12

Gabriel McAdams


You're pretty much there.

SELECT DISTINCT column INTO tableB FROM tableA

It's going to insert into whatever column(s) are specified in the select list, so you would need to alias your select values if you need to insert into columns of tableB that aren't in tableA.

SELECT INTO

like image 29
Justin Swartsel Avatar answered Dec 24 '22 16:12

Justin Swartsel