sql update query with space separated column name - mysql

Update Student Set First Name='adwd' Where StudentID=123;
ERROR 1064 (42000): You have an error in your SQL syntax;
This query is not working in mysql , because the column name in my table is space separated. if i do the query on another column which is not comma separated it will work.
However ,i want to keep the first name as is. I tried using [] and `` and "" and '' none of them have worked. Is this just impossible to do and i have to rename my column names ?
I hope some1 can provide a good solution
thx community :)

try this
Update Student Set `First Name`='adwd' Where StudentID=123;

If you are using MySQL, the correct syntax is:
Update Student
Set `First Name` = 'adwd'
Where StudentID = 123;
If this doesn't work, then something else is wrong with the query apart from the column name. Note that backticks are used for escaping the column name in MySQL.

Related

Truncated incorrect integer value MySQL

I have a table ABC that has many columns, two of which are:
ID - VARCHAR(10) and ROLE VARCHAR(10).
Now I have been trying to update the column ROLE using the ID and this is the query:
UPDATE TABLE ABC
SET ROLE='READ_ONLY'
WHERE ID='AB234PQR'
Now for some unknown reason, i have been getting the error - truncated incorrect integer value. I have no idea where I am going wrong.I have been banging my head over this for a while now.
I have visited other questions with the similar title.All use convert or some other function in where clause, But I have not used any such thing, still it gives me the same error.
I checked the table description and it seems fine. Where can I be going wrong? Any help is appreciated.
Can you please try this:
UPDATE ABC
SET ROLE='READ_ONLY'
WHERE ID='AB234PQR'
The correct syntax to update table entries is:
UPDATE `table_name`
SET `column_name` = 'value'
WHERE `column_name2` = 'value2';
It is as well recommended to use backticks around table names and column names, just like the way you can see in my snippet above.
Therefore using
UPDATE `ABC`
SET `ROLE` = 'READ_ONLY'
WHERE `ID` = 'AB234PQR'
should do the trick.

Simple SELECT SQL statement not working

SELECT user FROM members WHERE admin=1;
SELECT user FROM members WHERE id=1;
The first query does not work because there's an error in my syntax. The second works but they are virtually identical. Both 'admin' and 'id' are INT(11), but 'id' is the primary key and set to auto-increment. What's wrong here?
The error I receive is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'admin=1' at line 1
When you use keywords or reserved words don't forget to add quotes or brackets.
SELECT [user] FROM members WHERE [admin]=1;
SELECT [user] FROM members WHERE id=1;
I apologize for the confusion everyone, my fault. I did this and it works.
SELECT user FROM members WHERE members.group=2;

MySQL Update error while updating table

I have table in MySQL with years as column names like 1960, 1961, 1962... etc. Records are being inserted successfully. When I try to update table with query
UPDATE table1 SET 1960=0.0 WHERE id = 'abc'
it gives:
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '1960=0.0 WHERE id='abc' at line 1
Is it due to columns names as numbers or something else is wrong?
try this:
UPDATE table1 SET `1960`='0.0' WHERE id = 'abc'
... added backticks to column name and single quotes around value (not really required for the value, but I always do it)
escape your column name with backticks
UPDATE table1 SET `1960` = 0.0 WHERE id = 'abc'
That has to be done if your column name is a reserved keyword in MySQL or a number.
You'll have to escape your column names by using the backtick character. The following manual page is somewhat dense but informative
Try...
UPDATE table1 SET `1960`=0.0 WHERE id = 'abc'

Rename table with spaces mysql

I have a table 'A B C' (with spaces, don't ask me why) in MySQL database.
I have to rename it to 'ABC'
This query doesn't work :(
rename table 'A B C' to 'ABC'
What should be the correct query?
I get the same usual error
'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version'
Escape the name with backticks.
rename table `A B C` to ABC
Use backticks:
rename table `A B C` to ABC;
You probably just need to enclose the name w/spaces with tick marks (`) instead of single (') quotes. I don't know how to escape ticks so they show in the code preview but I think you will be able to figure it out.
Go to the sql query there and copy the nme of the table, after that paste back it in the query and enter your new name
Example : RENAME TABLE 'sales record' to sales
the 'sales record' need to copy from the sql original query there

Mysql syntax error during an update but code look fine

I have a strange issue with a Mysql Update and I'm not sure what is causing it. I suspect something is wrong with the table itself but the field causing the error appears to be defined the same as other fields in the table. I can recreate the error in phpMyAdmin on the SQL tab and also in php code. I am completely stumped.
The fields in the table are defined as follows:
bnumber is INT length=11
bname is VARCHAR length=60 Collation=latin1_swedish_ci
twittername is VARCHAR length=15 Collation=latin1_swedish_ci
desc is VARCHAR length=60 Collation=latin1_swedish_ci
This update statement works:
update tbl1 set bname='myName', twittername='myTweet' where bnumber=1;
this one gives me the error:
update tbl1 set bname='myName', twittername='myTweet', desc='test' where bnumber=1;
the error I get is:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc='Main' where bnumber=1' at line 1.
I don't seem to have any issues selecting from the table or inserting to the table. Only update is giving me the error.
I appreciate any help.
Thanks!
desc is a reserved word in MySQL. You must quote it in backticks.
desc is a keyword. Escape it with backticks.
update tbl1
set bname='myName',
twittername='myTweet',
`desc`='test'
where bnumber=1;
desc is a reserved word in mysql
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html
Updated your sql as below and it will work fine;
update tbl1 set bname='myName', twittername='myTweet', `desc`='test'
where bnumber=1;
you cant use desc as a field name.
change fieldname to desc1 and try.
desc is for oder by name desc