postgresql: INSERT INTO … (SELECT * …,"fixed value")

Visahan

The following is an example for the requirement I have.I have 2 tables. User table and another table where I enter the food user likes.

Class User{
    long id
}

class UserFood{
    Long id (auto-generated)
    long user_id
    String food
}

In my db I have a few hundred records. Now I want to take all the user's from UserFood table who have the food "French Toast" and add "Orange Juice"

So i'm looking for an insert statement like the following

insert into user_food (user_id,food) values (select user_id from user_food where food="French Toast","Orange Juice");

When i run the above query i get the following error

syntax error at or near "select"

Is there a solution for this?

Thanks in advance

Tim Biegeleisen

The following query will add orange juice records for all users who have at least one french toast record.

INSERT INTO user_food (user_id, food)
SELECT DISTINCT user_id, 'Orange Juice'
FROM user_food
WHERE user_id IN (SELECT user_id FROM user_food WHERE food = 'French Toast')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Insert fixed a value in a joined table

From Dev

postgresql insert into from select

From Dev

Insert into … values ( SELECT … FROM … ) in postgresql?

From Dev

Postgresql: INSERT INTO using SELECT and values

From Dev

Postgresql Insert select with multiple rows

From Dev

PostgreSql INSERT FROM SELECT RETURNING ID

From Dev

Insert from Select Postgresql with primary key constraint

From Dev

postgresql insert values from a select query

From Dev

PostgreSQL INSERT FROM SELECT with additional column

From Dev

INSERT based on a SELECT not inserting correctly with PostGreSQL

From Dev

PostgreSQL insert select multiple column values

From Dev

How to include the values of a select statement in an insert? (PostgreSQL)

From Dev

INSERT INTO MySQL table SELECT FROM PostgreSQL table

From Dev

PostgreSQL INSERT based on SELECT results using Python

From Dev

PostgreSQL INSERT FROM SELECT with additional column

From Dev

PostgreSQL; Using a SELECT sub-query in an INSERT

From Dev

PostgreSQL: How to insert, select, and update values with underscores

From Dev

Postgresql - Insert when select return something

From Dev

MSSQL Insert Data From One Table To Another With Fixed Value

From Dev

Set a fixed value for a field in a multi-row insert in Laravel

From Dev

Select the row conditioning on the first occurence of a fixed value using R

From Dev

Adding two select statements into one insert into statement in postgresql

From Dev

Insert & Select

From Dev

PostgreSQL - insert rows based on select from another table, and update an FK in that table with the newly inserted rows

From Dev

PostgreSQL INSERT or UPDATE values given a SELECT result after a trigger has been hit

From Dev

PostgreSQL - Multiple select primary key from three tables and insert as foreign key into one table

From Dev

how to select in select postgresql

From Dev

Insert row into a postgresql table

From Java

Insert, on duplicate update in PostgreSQL?

Related Related

  1. 1

    Insert fixed a value in a joined table

  2. 2

    postgresql insert into from select

  3. 3

    Insert into … values ( SELECT … FROM … ) in postgresql?

  4. 4

    Postgresql: INSERT INTO using SELECT and values

  5. 5

    Postgresql Insert select with multiple rows

  6. 6

    PostgreSql INSERT FROM SELECT RETURNING ID

  7. 7

    Insert from Select Postgresql with primary key constraint

  8. 8

    postgresql insert values from a select query

  9. 9

    PostgreSQL INSERT FROM SELECT with additional column

  10. 10

    INSERT based on a SELECT not inserting correctly with PostGreSQL

  11. 11

    PostgreSQL insert select multiple column values

  12. 12

    How to include the values of a select statement in an insert? (PostgreSQL)

  13. 13

    INSERT INTO MySQL table SELECT FROM PostgreSQL table

  14. 14

    PostgreSQL INSERT based on SELECT results using Python

  15. 15

    PostgreSQL INSERT FROM SELECT with additional column

  16. 16

    PostgreSQL; Using a SELECT sub-query in an INSERT

  17. 17

    PostgreSQL: How to insert, select, and update values with underscores

  18. 18

    Postgresql - Insert when select return something

  19. 19

    MSSQL Insert Data From One Table To Another With Fixed Value

  20. 20

    Set a fixed value for a field in a multi-row insert in Laravel

  21. 21

    Select the row conditioning on the first occurence of a fixed value using R

  22. 22

    Adding two select statements into one insert into statement in postgresql

  23. 23

    Insert & Select

  24. 24

    PostgreSQL - insert rows based on select from another table, and update an FK in that table with the newly inserted rows

  25. 25

    PostgreSQL INSERT or UPDATE values given a SELECT result after a trigger has been hit

  26. 26

    PostgreSQL - Multiple select primary key from three tables and insert as foreign key into one table

  27. 27

    how to select in select postgresql

  28. 28

    Insert row into a postgresql table

  29. 29

    Insert, on duplicate update in PostgreSQL?

HotTag

Archive