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

simple insert query not working

分類Dev

php mysql(INSERT QUERY)

分類Dev

MySQL query in PHP is not working

分類Dev

Mysql INSERT query inserts zeroes

分類Dev

MYSQL INSERT INTO FROM SELECT QUERY

分類Dev

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

分類Dev

Mysql Insert query with values and select as subquery

分類Dev

Insert multiple rows by one mysql query

分類Dev

Python Mysql CREATE and INSERT in one query

分類Dev

php insert mysql_query get id

分類Dev

Query working in mysql but not from c# code

分類Dev

Mysql query is not working but i could not guess why

分類Dev

Mysql join query help, not working as required

分類Dev

mysql query compare dates, DATEDIFF not working

分類Dev

Mysql select query working but giving error on update query

分類Dev

INSERT not working?

分類Dev

MYSQL data insert query doesn't work in Python

分類Dev

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

分類Dev

Mysqli Insert Command not Working

分類Dev

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

分類Dev

Insert query is failing

分類Dev

Manually insert resultset into query

分類Dev

Numeric overflow in insert query

分類Dev

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

分類Dev

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

分類Dev

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

分類Dev

MySQL query inside query

分類Dev

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

分類Dev

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

Related 関連記事

  1. 1

    simple insert query not working

  2. 2

    php mysql(INSERT QUERY)

  3. 3

    MySQL query in PHP is not working

  4. 4

    Mysql INSERT query inserts zeroes

  5. 5

    MYSQL INSERT INTO FROM SELECT QUERY

  6. 6

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

  7. 7

    Mysql Insert query with values and select as subquery

  8. 8

    Insert multiple rows by one mysql query

  9. 9

    Python Mysql CREATE and INSERT in one query

  10. 10

    php insert mysql_query get id

  11. 11

    Query working in mysql but not from c# code

  12. 12

    Mysql query is not working but i could not guess why

  13. 13

    Mysql join query help, not working as required

  14. 14

    mysql query compare dates, DATEDIFF not working

  15. 15

    Mysql select query working but giving error on update query

  16. 16

    INSERT not working?

  17. 17

    MYSQL data insert query doesn't work in Python

  18. 18

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

  19. 19

    Mysqli Insert Command not Working

  20. 20

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

  21. 21

    Insert query is failing

  22. 22

    Manually insert resultset into query

  23. 23

    Numeric overflow in insert query

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

    MySQL query inside query

  28. 28

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

  29. 29

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

ホットタグ

アーカイブ