I would like to concatenate a column and its index (numerical name) inside of a SELECT statement in MySQL.
For example there are 4 columns: (Column1, Column2,Column3...ColumnN). Instead of updating the values by referring to them by name, I hope to use a variable named 'index' and attach it to the end of the word "Column".
e.g. (Column(index),Column(index)...)
I have tried the CONCAT() and CONCAT_WS functions from the MySQL documentation.
For example, if I set the value of the variable index = 3, it results in the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '('Column','3')
The alternative would be to redesign database and its relations and I wish to avoid this.
Is what I am attempting possible for column names or simply strings within the database?
Edit:
$score_update = "UPDATE Users
SET CONCAT ('USER_module','$module_chosen_index') ='$module_quiz_score'
WHERE USER_firstname = '$username'";
The values of the $module_chosen_index can be (1,2...15) and the column title if I entered it and didn't use CONCAT() is USER_module3.
Related
update amazon-crawler set `flag_images`= '0' where `id`='966'
i get #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 '-crawler set flag_images= '0' where id='966'' at line 1
why syntax error?
amazon-crawler is the table
flag images and id are columns
My guess is that the hyphen in the table name is causing problems, because it is an arithmetic operator. Try also escaping the table name:
UPDATE `amazon-crawler` SET `flag_images`= '0' WHERE `id` = '966';
Note that you should try to avoid using backticks in your query, unless absolutely needed. Using backticks means that any name would potentially work, even one which happens to be a MySQL reserved keyword. Also, I'm guessing that flag_images and id are numeric columns, in which case you should be comparing them against numbers, not strings. So I would write your update as this:
UPDATE `amazon-crawler` SET flag_images= 0 WHERE id = 966;
Here, only the table name has to appear in backticks.
I have a Chef recipe for creating Unix user IDs and deploying them across multiple nodes, to guarantee uniqueness and prevent devs from having to track them themselves. They merely name the application and it is granted a unique ID if an ID for that application doesn't already exist. If one does, it is simply returned to the script and user accounts are created on the webservers with the appropriate value.
I have a mysql database with a single table, called application_id_table which has two columns, id and application_name. id is autoincrementing and application name cannot be null (and must be unique).
Removing the Ruby from my script and making a couple of substitutions, my sql looks like this:
INSERT INTO application_id_table(application_name) VALUES('andy_test')
WHERE NOT EXISTS (select 1 from application_id_table WHERE
application_name = 'andy_test');
when run, I receive the syntax parsing error:
ERROR 1064 (42000): 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 'WHERE NOT EXISTS (select 1 from
application_id_table WHERE application_name = 'a'
I recall seeing that the values statement does not allow a where clause but I don't wish to use a select statement to populate the values as I'm populating them from variables supplied from within Ruby/Chef. Anyone have an idea how to accomplish this?
You want to use insert . . . select:
INSERT INTO application_id_table(application_name)
SELECT aname
FROM (SELECT 'andy_test' as aname) t
WHERE NOT EXISTS (select 1 from application_id_table ait WHERE ait.application_name = t.aname);
You should be able to plug your variable directly into the select statement, the same you would would with the VALUES statement.
Try this:
INSERT INTO application_id_table(application_name)
select 'andy_test'
WHERE NOT EXISTS (select 1 from application_id_table WHERE application_name = 'andy_test');
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
Database name : test
table names : order, order_shipping,order_payment
The query below gives me error
INSERT INTO order(order_status,customer_id) values('booked',1)
error : 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 'order(order_status,customer_id) values('booked',1)' at line 1
But the exact same query will work if i add database name before tablename
INSERT INTO test.order(order_status,customer_id) values('booked',1)
result : insertion successfull
I renamed tablename 'order' to 'order_main' and it works without database name
INSERT INTO order_main(order_status,customer_id) values('booked',1)
insertion successfull
My question is why does not my original query work without database name attached to table name. Is it because I have other tables starting with this table name ???
table in my database : order, order_shipping,order_payment
order is a reserved keyword within MySQL. If you want to use it as an identifier, enclose it in backticks:
INSERT INTO `order` (order_status,customer_id) values('booked',1)
In the second query, you specify a full identifier, which MySQL does not mistake for a keyword. Hence, it works without problems.
can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
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 '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).
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'