mysql insert select syntax error - mysql

The following SQL statement is valid and executes as expected:
SELECT r1.rs_objectidentifier AS parent,
r2.rs_objectidentifier AS child,
"satisfies" AS relation,
0 AS root,
0 AS leaf
FROM rsdata r1,
rsdata r2
WHERE r1.weeknumber="1410"
AND r1.weeknumber=r2.weeknumber
AND r1.rs_variant=r2.rs_variant
AND r1.rs_inrsobjectidentifieronelevel != ""
AND r1.rs_inrsobjectidentifieronelevel != "Unknown"
AND find_in_set(r2.rs_objectidentifier, r1.rs_inrsobjectidentifieronelevel)
UNION
SELECT rsdata.rs_objectidentifier AS parent,
"" AS child,
"satisfies" AS relation,
0 AS root,
0 AS leaf
FROM rsdata
WHERE rsdata.weeknumber="1410"
AND (rsdata.RS_InRSObjectIdentifierOneLevel = ""
OR rsdata.RS_InRSObjectIdentifierOneLevel = "Unknown")
when I prepend the select with the following insert statement, I get an SQL syntax error:
insert into traceability
The columns in the select match the fields in the table (as far as I can tell). The table looks like:
CREATE TABLE `traceability` ( `Parent` varchar(50) default NULL,
`Child` varchar(50) default NULL, `Relation` varchar(20) default
NULL, `Root` tinyint(1) default NULL, `Leaf` tinyint(1) default
NULL, UNIQUE KEY `Traceability_UI1` (`Parent`,`Child`,`Relation`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1
Why do I get a MySQL syntax error when the insert statement as added???
The following error (not very informative) 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 'insert into
traceability select r1.rs_objectidentifier as parent, r2.rs_objec' at line 2

Try this
insert into traceability(Parent,Child,Relation,Root,Leaf) Select .....

OK. Solution was very simple. The SQL is part of a set of SQL statements. The statement posted above was missing a ; (to terminate the statement) so was running into the next statement and therefore throwing a syntax error.... hangs head in shame

Related

Why am I getting an insert blob error when using MySql v8.0 instead of v5.1?

I created the following table:
CREATE TABLE `player__main` (
`row` varbinary(256) NOT NULL,
`schema_id` int NOT NULL,
`version` int NOT NULL,
`format` tinyint NOT NULL,
`avro` mediumblob NOT NULL,
PRIMARY KEY (`row`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Then, I run my script below on MySql v5.17:
insert into player__main (row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69');
Query OK, 1 row affected (0.05 sec)
However, when I execute the code below in MySql v8.0:
insert into player__main (row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69');
I get this 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 'row, schema_id, version, format, avro) VALUES
(x'61646D696E',11,1,0,x'0A61646D69' at line 1
It's the same query but it returns different results depending of the version of MySql I am using.
How can I fix this?
I think the real problem here is row is a reserved keyword in 8:
ROW (R); became reserved in 8.0.2
You'll need to rename that column, or escape it going forward:
INSERT INTO player_main (`row`, ...)

Error using IF statement in MySQL

I'm trying to create a mySQL Script file, so i can easily execute my changes in differente databases, but I'm having problems with a IF statement. (I'm using MySQL Workbench).
CREATE TABLE IF NOT EXISTS `build` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Version` varchar(20) NOT NULL,
`Date` datetime DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
IF EXISTS(select 1 from build where Versao = '1.1') THEN select "yes";
The create table sentence is executed correctly, but the "IF Exists(select..." statement is giving the following error:
SYNTAX ERROR: IF (if) is not a valid input at this position
The select "Yes" command will actually be replaced by an insert command. I'm just trying to test if the IF command will work.
I've also tried to put the IF EXISTS(select line in a separated query, but had the same result.
What am I doing wrong?
The SQL IF function as I imagine you want to use should be used inside a function, which you never declared.
If you want a standalone query which will behave the way you expect, then you can try this:
SELECT CASE WHEN EXISTS (SELECT 1 FROM build WHERE Versao = '1.1')
THEN "yes"
ELSE "no"
END
LIMIT 1
SQL functions can only be used inside a statement or inside a stored procedure/routine.
This SELECT statement should work for you:
SELECT IF(EXISTS(SELECT 1 FROM build WHERE Versao = '1.1'), 'yes', 'no') AS test;

Unknown syntax error in MySQL statement

I am using below CREATE TABLE statement
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(MAX) NOT NULL,
PRIMARY KEY (`uuid`)
);
However I keep getting this 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
'MAX) NOT NULL,
PRIMARY KEY (uuid)
)' at line 3
Makes no sense to me.
MAX is not suported for this use, it is reserved for the MAX function. Use the equivalent number instead, check this out: Equivalent of varchar(max) in MySQL?
This will work for you. MAX is reserved keyword. Specify exact number of varchar instead of max. However, varchar(MAX) will work in SQL SERVER 2005+.
CREATE TABLE IF NOT EXISTS users (
uuid varchar(36) NOT NULL,
json varchar(21808) NOT NULL,
PRIMARY KEY (uuid)
);
FIDDLE
MAX() is a function in MySql,So if you want to declare the size to the max.please refer the below example.
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(65535) NOT NULL,
PRIMARY KEY (`uuid`)
);
and if you calculate that 21845*3 = 65535, which wouldn't have worked anyway. Whereas 21844*3 = 65532, which does work.
mysql> CREATE TABLE foo ( v VARCHAR(21844) ) CHARSET=utf8;
Query OK, 0 rows affected (0.32 sec)

MySQL Table Template w/ FUNCTION/PROCEDURE

I am creating several logbooks on the same siteā€”all with the same structure, so when I need to add a new logbook, I'd like to be able to do so with a simple function or something (since the table-structure will be exactly the same.)
DELIMITER //
CREATE FUNCTION GenerateNewLogbook(LogbookTitle varchar(15))
BEGIN
DROP TABLE IF EXISTS LogbookTitle;
CREATE TABLE LogBookTitle (
time timestamp NOT NULL DEFAULT NOW(),
username varchar(25) DEFAULT NULL,
message text,
crossout tinyint(1) NOT NULL DEFAULT '0',
post_id mediumint(9) NOT NULL AUTO_INCREMENT,
file_id text,
PRIMARY KEY (post_id)
) ENGINE=InnoDb AUTO_INCREMENT=0 DEFAULT CHARACTER SET=latin1;
END//
DELIMITER ;
The error I'm getting is:
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 'BEGIN
DROP TABLE IF EXISTS LogbookTitle;
CREATE TABLE LogBookTit' at line 2
I've tried many things: Replacing the ";" with "//", creating a PROCEDURE instead of a FUNCTION, adding "#" before LogBookTitle, removing BEGIN/END, etc. and have spent quite a while searching for examples to no avail.
What's wrong with the above code?

mysql 5.3 - some error, but query seems ok

mysql query - 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 'a', '1') ON DUPLICATE KEY UPDATE count=count+1' at line 1
The query that fails:
INSERT INTO tags (ip, tag, count)
VALUES ('xx.xx.xxx.xxx', 'krwiopijcy', '1')
ON DUPLICATE KEY UPDATE count=count+1;
Is there anything wrong in my query?
count is a built in function and as such, may require care to be used as such. However, as the docs say, it is perfectly fine as column name. You might want to escape it using
`count`
That being said, I had no problem with your exact query (no escaping) using this table structure:
CREATE TABLE `tags` (
`ip` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`tag` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`count` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Your example from the comments has one apostrophe too much (behind 'umacku'), which doesn't seem to be the problem (as it is not given in the error message), but you should paste the exact queries:
INSERT INTO tags (ip, tag, count)
VALUES ('xx.xx.xxx.xxx', 'umacku'', '1')
ON DUPLICATE KEY UPDATE count=count+1;