What is the bug in the MySQL query? - mysql

I have the MySQL INSERT ROW below. For some reason I keep getting a syntax error with this. Any ideas? I have checked my table multiple times to make sure those table columns exist.
INSERT INTO content_pieces (content_id, order, piece, type) VALUES ('$content_id', '$key', '$indiv_piece', '$piece_attr')

order is a reserved word in mySQL.
You need to wrap it in backticks:
(`content_id`, `order`, `piece`, `type`)
or - better - use a different column name.

Backtick your column values order is a reserved SQL keyword.

Related

INSERT INTO statement in a specific column

For example i have table with a different field names(column), lets say 5 columns and all of them are empty. And i wanted to insert data in one specific column. Is it possible? I'm looking for example of this, but unlucky to find one. Most of insert into statements examples required all columns to be filled. If possible, can you give me the correct syntax? I'm sorry if i'm lacking research or it's already been asked, it's ok if you will redirect me to the link.
If you want insert on column3, leaving empty the other:
INSERT INTO table_name (column1,column2,column3,column4,column5)
VALUES ("","","VALUE","","");
The other part of program would UPDATE the other columns:
UPDATE table_name
SET column1=value1,column2=value2,column4=value4,column5=value5
WHERE some_column=some_value;
The documentation on how to construct an INSERT INTO statement is here: INSERT INTO Statement (Microsoft Access SQL).
But basically, you just need to be explicit about which columns you want to insert values for, and omit the other ones, like this:
INSERT INTO table (colname) VALUES ('colvalue')
What happens to the fields you omit? The documentation says:
When you do not specify each field, the default value or Null is inserted for missing columns.

mysql insert into query not working

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));
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;
DEFAULT is a MySQL reserved word. You may need to enclose that column name in backticks.
INSERT into controls (`default`, whitelist,
^ ^
Those are backtick characters (key to the left of the 1/! key), not single quotes.
The error message from MySQL should indicate where in the SQL text MySQL thinks the problem is.
If this is a new table, strongly consider using a column name that is not a MySQL reserved word.
Reference: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
The SELECT in the context of the VALUES clause needs to return at most one row. You'd need to ensure that SELECT doesn't return more than one row, e.g. add a LIMIT 1 clause. But
Reading your question again... if you want to insert a row in the new controls table for every row that's in users table, don't use the VALUES keyword. Use the INSERT ... SELECT form of the INSERT statement.
Reference: http://dev.mysql.com/doc/refman/5.5/en/insert-select.html

why is mySQL missing a column?

This statement is not populating the image_path column. I have copied and pasted column names from phpMyAdmin to ensure accuracy.
INSERT INTO (theme_name, theme_path, image_path)
VALUES ('',
'/Applications/MAMP/htdocs/waggleb/wp-content/plugins/app-switcher/themes/Original/',
'/Applications/MAMP/htdocs/waggleb/wp-content/plugins/app-switcher/thumbnails/original.png')
You're missing a table name. If you look at the MySQL INSERT syntax, you'll see that it's mandatory to include a table name.
As such, if you alter your query to become..
INSERT INTO <TABLE NAME> (theme_name, theme_path, image_path) VALUES...
...all should be well.
you missed the table name
INSERT INTO TABLENAME(theme_name, theme_path, image_path)
VALUES ('',
'/Applications/MAMP/htdocs/waggleb/wp-content/plugins/app-switcher/themes/Original/',
'/Applications/MAMP/htdocs/waggleb/wp-content/plugins/app-switcher/thumbnails/original.png')
I believe you are missing the table name.
It should be:
Insert Into [Table_name]
([Columns])
Values ([Values])
You've forgotten to name a table to INSERT into:
INSERT INTO my_table (theme_name, theme_path, image_path)
If you omitted the table name on purpopse, check that the datatypes are correct for the columns. Check lengths of target columns are long enough to hold what you are inserting.

Mysql INSERT Statement Differences

I normally found three ways of using MYSQL insert Command
INSERT INTO `table` (field1, field2, field3) VALUES ('value1', 'value2', 'value3');
or
INSERT INTO `table` VALUES ('value1', 'value2', 'value3');
or
INSERT INTO `table` SET `field1`='value1', `field2`='value2', `field3`='value3';
Is there any differences among these?
The first two are standard SQL; the third one is non-standard and specific to MySQL, derived from the standard syntax for UPDATE.
The difference between the first two is that one specifies the fields that you want to insert, which is more flexible because you can miss out fields if you're happy with them being set to NULL. Also specifying the fields means that the statement will still work if the table layout changes; if you don't specify the fields you're relying on the table never changing.
You are looking for differences in terms of what? In terms of usage, the first and third insert statements let you to set only those columns which you need whereas the second statement always tries to insert the values to all columns in order of table structure.
Only one difference, pretty obvious to me: the second one don't let you specify your own field set to insert.

MySQL INSERT INTO ... SELECT throws Error 1064

Trying to duplicate some rows in a table but just change the ssreportid column from 4 to 6:
INSERT INTO ssreportparticipant (ssreportid, sssurveyparticipantid)
VALUES
SELECT 6, sssurveyparticipantid FROM ssreportparticipant
WHERE ssreportid = 4
The error says #1064 near 'select 6, ...' but if I just run the select clause, it selects the records perfectly, with the new id of 6 in the ssreportid column.
The table has a primary key called ssreportparticipantid, and there is a unique key on (ssreportid, sssurveyparticipantid). Note that the select clause creates new records that have unique key pairs, so that's not the problem. I have tried putting brackets around the select clause, and even using two aliases for the table, no joy.
Using server version 5.0.45.
Please tell me that programmer fatigue has me missing a comma or something.
Thanks,
-Josh
I think you should remove "VALUES"
I am not sure, but maybe it is possible that you cannot insert into a table with a select from the same table. Have you tried to select from a different table, just for the sake of testing ?