MySQL Update nth character in existing entry - mysql

How do I update a single character, in table 'units', column 'status' (varchar), at a specific position (varies)?
UPDATE units SET status = SELECT INSERT(status, 3, 1, '2') WHERE id='1'
When running this (in PHP) This give me an error 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version....'
Rather than the desired
Before '00000'
After '00200'
There are lots of examples of the SELECT INSERT function but all with a predefined string, rather than using an existing entry.
Thank you for your consideration.

Just remove the SELECT before INSERT(...
UPDATE units SET status = INSERT(status, 3, 1, '2') WHERE id='1'

Related

Attempting to insert data into a column

I have a table called design_designs
The table contains 4 columns: id, key, value, nonceId
I'm attempting to run a query to insert into the table:
INSERT INTO design_designs(key, value, nonceId)
VALUES ('test key', 'test value', 'test nonce');
The error i get is:
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 'key, value, nonceId)
VALUES ('test key', 'test value', 'test nonce')' at line 1
Any idea what I'm doing wrong? According to the documentation on queries my query is correct. I'm obviously missing something.
key is a reserved word, you must delimit it:
INSERT INTO design_designs(`key`, value, nonceId) VALUES...

Copying everything from one table to another - Change a value, and have an auto incrementing ID?

I am trying to copy the entire contents of "TableA" to "TableB"... but with a few catches, first off, Table B needs to have all new auto incrementing IDs (I have it set as a BIGINT, and it auto increments in the schema), then I need the value of "refereceID" in TableB to always have the "id" value of that row in TableA. Third, there is a "report" field, and for now - I would like it to ALWAYS have the value of '1'.
Here is my current SQL :
INSERT into tableB(id, report, referenceID, address, zip, last_seen_on, created)
VALUES(
,1
,[id]
,[address]
,[zip]
,[last_seen_on]
,[created])
SELECT * FROM TableA
But I keep getting an SQL error which looks like this :
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 ' 1, [id], [address], [zip], [last_seen_on], [created] ' at line 11
I think this is what you need (note this syntax is for SQL, missed the MySQL part, but should be very similar/the same for MySQL)
INSERT into tableB(report, referenceID, address, zip, last_seen_on, created)
Select 1
,[id]
,[address]
,[zip]
,[last_seen_on]
,[created]
from TableA
Your insert has more columns then your values (if id is the auto increment value in your Insert statement, you should be able to leave that off).

Mysql IF EXISTS THEN

So--i'm having an issue with my code. I'm testing it directly in the console and getting an "syntax error"
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 'IF EXISTS(SELECT * FROM user_inventory WHERE resource_id = '6'
AND uid ='1') T' at line 1
IF EXISTS(SELECT * FROM user_inventory WHERE resource_id = '6' AND uid ='1')
THEN UPDATE user_inventory SET resource_count = resource_count+1 WHERE resource_id = 6 AND uid = 1
ELSE INSERT INTO user_inventory(uid, resource_id, resource_count) VALUES (1, 6, 1);
I've never used the IF EXISTS clause before... So I'm not sure what i've done wrong.
The SQL IF statement can only be used within a stored program. You can't use it within a generic SQL context as you have attempted.
However, you do not need to use IF here:
Define a suitable uniqueness constraint on your user_inventory table:
ALTER TABLE user_inventory ADD UNIQUE (uid, resource_id)
Use INSERT ... ON DUPLICATE KEY UPDATE:
INSERT INTO user_inventory
(uid, resource_id, resource_count)
VALUES
(1, 6, 1)
ON DUPLICATE KEY UPDATE
resource_count = resource_count + 1

Mysql insert statement where one value is given and on obtain from a subquery

I have the following query:
INSERT INTO `area`(name, fk_hub_id) VALUES ('$name',SELECT 'id' from hub WHERE name = '$hub_name')
What i am trying to do is insert a value (name= $name) which is obtained from a from and then the foreign key id of a certain hub which is obtained by the sub-query.
when I run this query i get the following 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 'SELECT 'id' from hub WHERE name = 'EASTERN_CAPE')' at line 1.
Is my query incorrect? Any suggestions how to do it?
Use ` around column_name, you are using ' around id column_name. Also use () for inner query.
INSERT INTO `area`(name, fk_hub_id) VALUES ('$name',SELECT 'id' from hub WHERE name = '$hub_name')
should be
INSERT INTO `area`(name, fk_hub_id) VALUES ('$name',(SELECT `id` from hub WHERE name = '$hub_name'))
Note : Your code is wide open for SQL injection, use prepared statement OR escape them.
Put the given value into the SELECT:
INSERT INTO area (name, fk_hub_id)
SELECT '$name', id
FROM hub
WHERE name = '$hub_name'
Try:
INSERT INTO `area` (name, fk_hub_id)
SELECT '$name',
id
FROM hub
WHERE name = '$hub_name'

Error on MySQL Syntax

I'm trying to teach myself MySQL while working on a project at the same time. I'm using phpMyAdmin.
I'm getting the 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 ''ps_category' ('id_category', 'id_parent', 'id_shop_default', 'level_depth', 'nl' at line 1"
My code:
INSERT INTO 'ps_category'
('id_category', 'id_parent', 'id_shop_default',
'level_depth', 'nleft', 'nright', 'active',
'date_add', 'date_upd', 'position', 'is_root_category')
VALUES (6,2,1,0,0,0,1,'2012-04-12 15:12:54','2012-04-12 15:12:54',1,0)
UPDATE:
I took off the single quotes and still getting the same error:
INSERT INTO ps_category
('id_category', 'id_parent', 'id_shop_default',
'level_depth', 'nleft', 'nright', 'active',
'date_add', 'date_upd', 'position', 'is_root_category')
VALUES (6,2,1,0,0,0,1,'2012-04-12 15:12:54','2012-04-12 15:12:54',1,0)
INSERT INTO `ps_category` (`id_category`, `id_parent`, `id_shop_default`, `level_depth`, `nleft`, `nright`, `active`, `date_add`, `date_upd`, `position`, `is_root_category`) VALUES (6,2,1,0,0,0,1,'2012-04-12 15:12:54','2012-04-12 15:12:54',1,0)
You are using a single quote on the table name. It should be ticks or nothing. It should be noted, the ticks help to ensure properly reading the table name. If you name your table a mysql reserved word, the ticks will prevent it from erroring
The table name should not be entered as a string literal, either remove these '' or put two '' and '' around it like so
INSERT INTO ps_category ...
Or
INSERT INTO `ps_category` ...
Table names should not be quoted