Questions tagged [insert-select]

Use this tag for questions concerning INSERT, the SQL command which inserts data into a table using a SELECT query. Questions should also be tagged [sql] and, where applicable, tagged with the database engine in use.

This tag is used for questions dealing with the SQL INSERT command, where the data is inserted into a table using a SELECT query as shown below:

INSERT INTO <table name>
SELECT <column names> 
FROM <source table>
.....
130 questions
1
vote
4 answers

How to speed up INSERT SELECT with join over string

I want to call insert select and I try to use this select (with help from this INSERT SELECT query when one column is unique) SELECT minids.userid, username, password, full_name, country, email, (select openclipart_files.id from aiki_users,…
jcubic
  • 61,973
  • 54
  • 229
  • 402
1
vote
2 answers

How to copy a table and add a field with MySQL?

I would like to copy a table A into a table B with INSERT ... SELECT without declaring all fields in MySQL. But the issue is my table B has an auto-increment field at the beginning called id: table_A name age etc -------|--------|--------| …
Dacobah
  • 779
  • 3
  • 15
  • 35
0
votes
3 answers

php mysql insert select multiple with where clause

I have a following Table1 : userid, who, share, date :: id is auto increment and primary key I would like to build a query to check if record exist and insert new record if is empty. How to build this in a single query and insert multiple records…
wyknzo
  • 81
  • 2
  • 10
0
votes
1 answer

INSERT INTO...SELECT...WHERE with null if condition fails

I'm attempting to do something like the following: INSERT INTO `File` (`Name`, `Owner`) SELECT @File, `Users`.Id FROM `Users` WHERE `Users`.`Name` = @UserName However, if there is no match in the Users table, I would like it to still insert the…
Kevin Fee
  • 495
  • 4
  • 17
0
votes
1 answer

Insert rows into different table in MYSQL

I'm trying to re-map some data within my mysql database. I want to move content within a table called category_article to another table called article_tag. I want to move a single field (article_id) and add my own data for tag_id. I currently…
Andy Tait
  • 199
  • 2
  • 4
  • 10
0
votes
1 answer

PostgreSQL: copy the table rows and get the old-new matching information

Here we have a certain table: CREATE TABLE mytbl ( id int PRIMARY KEY generated by default as identity, col1 int, col2 text, ... ); We need to copy part of the rows of the table and get information about the correspondence of the old and new…
0
votes
2 answers

Avoid duplicate keys on INSERT INTO/SELECT

I am trying to do an Insert/Select but I am getting a duplicate key error. INSERT INTO dbo.DESTINATION_TABLE ( DocNumber, TenantId, UserId, OtherField …
sheamus
  • 3,001
  • 4
  • 31
  • 54
0
votes
1 answer

column "birthdate" is of type timestamp without time zone but expression is of type text

This query throws an error: column "birthdate" is of type timestamp without time zone but expression is of type text INSERT INTO profile.profile(name,gender,birthDate,userId) SELECT userId, substr(md5(random()::text), 0,…
Mistral
  • 5
  • 1
  • 3
0
votes
1 answer

SQL - INSERT SELECT two columns without duplicates

I am trying to do two subqueries to populate my table with two columns. wk_start and wk_end. Goal is to populate it with a specific range of date. I've tried cross join but still having duplicates the same as this one Here is my code insert into…
Drenyl
  • 906
  • 1
  • 18
  • 41
0
votes
1 answer

SQL INSERT/SELECT using WITH clause

I'm trying to make a copy from two tables based on a list of IDs, those IDs are retrieved from an initial table, then those IDs are used into an INSERT SELECT statement, also the created IDs from the previous INSERT must be inserted into a third…
Diego Nieto
  • 581
  • 2
  • 10
  • 23
0
votes
1 answer

Postgres Insert select using WITH clause

I'm trying to create some entries by using an INSERT ... SELECT ..., but also need to use the primary key from the 1st INSERT operation to be included as part of a secondary operation: WITH dogEntries as (INSERT INTO dog (id, another_id, name,…
Diego Nieto
  • 581
  • 2
  • 10
  • 23
0
votes
2 answers

How to get max(column_name) from a table irrespective of a where clause in the query?

I have a requirement of inserting data into a table and one of the columns is seq_number. I have a where clause in the select query and I want to insert the max(seq_num)+1 for every record that I want to insert. The max of seq_num is giving me the…
0
votes
1 answer

How do you create a row in a table without using the VALUES key word in T-SQL

I got asked an interview question that I haven't a clue how to answer and was curious how you would write a script for something like this. "Write a statement that creates a row representing you in the Employees table without using the VALUES key…
YHapticY
  • 177
  • 11
0
votes
2 answers

How to insert values to table where one column needs a single value from select statement and another column needs multiple values from another select

Table1 product: productname , productID Table2 alias: product_alias, components, id Table3 mapping: productId, alias_component_id INSERT INTO `mapping`(`productId`,`alias_component_id`) VALUES ((SELECT productID FROM product WHERE NAME='abc'), …
0
votes
3 answers

INSERT INTO SELECT from UPDATE

I need to select some values from a table to be updated, and then update them right away. Furthermore, I need to insert one new record in a table for each updated record. To select records and update I am using a structure like UPDATE TableA SET…
B.M
  • 533
  • 2
  • 8
  • 16
1 2 3
8 9