mysql insert into query not working

Shail Patel

The query is pasted below. Basically I have a users table, and I want to create 1 record for every entry in the users table in the controls table. I need the user_id from the users table when I'm inserting into the controls table. I get a syntax error when I try and run the query below. I've looked at a bunch of other MySQL examples that do something similar but get the same error. I'm running MySQL 5.6.21.

This will end up being a Rails migration, so I am also amenable to using ActiveRecord, but haven't really figure out how to do something like this with it.

INSERT into controls (default, whitelist, user_id, admin_status, system_status, created_at, updated_at) values (0, 0, (SELECT id FROM users), 'inactive', 'sleeping', DATE(NOW), DATE(NOW));
Mike K

I believe your problem is that you're trying to mix a SELECT with VALUES (which is used for inputting literal values).

Try:

INSERT into controls
    (`default`, whitelist, user_id, admin_status, system_status, created_at, updated_at)
    SELECT 0, 0, id, 'inactive', 'sleeping', NOW(), NOW() FROM users;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

MySql workbench query history ( last executed query / queries ) i.e. create / alter table, select, insert update queries

分類Dev

Spring Boot Data JPA NativeQuery throws Syntax-Error on Query working in MySQL Workbench

分類Dev

Comparing mysql password hashes using query row returning 0 (not working)

分類Dev

MySQL query in PHP is not working

分類Dev

How to get last id inserted. mysql_insert_id() not working

分類Dev

MYSQL data insert query doesn't work in Python

分類Dev

Mysql select query working but giving error on update query

分類Dev

simple insert query not working

分類Dev

Query working in mysql but not from c# code

分類Dev

Mysql INSERT query inserts zeroes

分類Dev

MySQL query inside query

分類Dev

Why is my SQL "INSERT INTO" query not working?

分類Dev

MySQL - NOT IN query in WHERE clause with same structure working on first table but not on second table

分類Dev

Insert query is failing

分類Dev

php mysql(INSERT QUERY)

分類Dev

Manually insert resultset into query

分類Dev

Mysql query is not working but i could not guess why

分類Dev

Mysql Insert query with values and select as subquery

分類Dev

Mysql join query help, not working as required

分類Dev

Mysqli Insert Command not Working

分類Dev

How can I insert multiple records in mysql using php at once with one query dynamicaly?

分類Dev

INSERT not working?

分類Dev

Insert multiple rows by one mysql query

分類Dev

Couldn't be able to insert data into MySQL even if I enter write query

分類Dev

Python Mysql CREATE and INSERT in one query

分類Dev

Numeric overflow in insert query

分類Dev

MYSQL INSERT INTO FROM SELECT QUERY

分類Dev

mysql query compare dates, DATEDIFF not working

分類Dev

php insert mysql_query get id

Related 関連記事

  1. 1

    MySql workbench query history ( last executed query / queries ) i.e. create / alter table, select, insert update queries

  2. 2

    Spring Boot Data JPA NativeQuery throws Syntax-Error on Query working in MySQL Workbench

  3. 3

    Comparing mysql password hashes using query row returning 0 (not working)

  4. 4

    MySQL query in PHP is not working

  5. 5

    How to get last id inserted. mysql_insert_id() not working

  6. 6

    MYSQL data insert query doesn't work in Python

  7. 7

    Mysql select query working but giving error on update query

  8. 8

    simple insert query not working

  9. 9

    Query working in mysql but not from c# code

  10. 10

    Mysql INSERT query inserts zeroes

  11. 11

    MySQL query inside query

  12. 12

    Why is my SQL "INSERT INTO" query not working?

  13. 13

    MySQL - NOT IN query in WHERE clause with same structure working on first table but not on second table

  14. 14

    Insert query is failing

  15. 15

    php mysql(INSERT QUERY)

  16. 16

    Manually insert resultset into query

  17. 17

    Mysql query is not working but i could not guess why

  18. 18

    Mysql Insert query with values and select as subquery

  19. 19

    Mysql join query help, not working as required

  20. 20

    Mysqli Insert Command not Working

  21. 21

    How can I insert multiple records in mysql using php at once with one query dynamicaly?

  22. 22

    INSERT not working?

  23. 23

    Insert multiple rows by one mysql query

  24. 24

    Couldn't be able to insert data into MySQL even if I enter write query

  25. 25

    Python Mysql CREATE and INSERT in one query

  26. 26

    Numeric overflow in insert query

  27. 27

    MYSQL INSERT INTO FROM SELECT QUERY

  28. 28

    mysql query compare dates, DATEDIFF not working

  29. 29

    php insert mysql_query get id

ホットタグ

アーカイブ