About SQL Errors (1064)

Asked 1 years ago, Updated 1 years ago, 34 views

Using HeidiSQL 9.4.0.5125 I copied 2-4 exactly from the following sites and changed the table name to tmptreagency and the column to name.
http://www.geocities.jp/mickindex/database/db_tree_ns.html#LocalLink-level

Then,

SQL Error (1064): You have an error in your SQL syntax. Check the manual
that responses to your MySQL server version for the right syntax to
use near INTEGER)+1,``)AS name FROM tmptregency Mgrs,
tmptreagency MidMgrs, tmpt' at line 1

says the error.

I have tried again and again, but the error has not been fixed.

Could you tell me the cause of the error?

This is an actual SQL statement

SELECT LPAD(Mgrs.name, LENGTH(Mgrs.name) + CAST(COUNT(*)AS INTEGER) +1,')AS name
  FROM tmptregency Mgrs, tmptregency MidMgrs, tmptregency Workers
 WHERE Mgrs.lft BETWEEN MidMgrs.lft AND MidMgrs.rgt
   AND MidMgrs.lft BETWEEN Workers.lft AND Workers.rgt
GROUP BY Mgrs.name, Mgrs.lft
ORDER BY MAX (Mgrs.lft);

sql

2022-09-30 21:24

2 Answers

It's like MySQL, so what about SIGNED instead of INTEGER?
When I tried only the CAST part, I got a syntax error with INTEGER.


2022-09-30 21:24

Thank you for adding SQL.However, this time it didn't seem to be your copy-editing error, so I ran the same SQL with PostgreSQL 9.3, and it worked fine.(I don't know if it's exactly what the original author intended because I didn't input the data, but at least it doesn't get SQL errors.)

Tested with MySQL 5.6 on SQL Fiddle (HeidiSQL itself is a tool that can be used outside of MySQL, so if you are using MySQL as a DB, you should show it with tags, etc.If you look at the error message carefully, you will understand…) and CAST(...AS INTEGER) are treated as syntax errors.
(After reading yud's post, I looked into it a little more and corrected it.In MySQL CAST, the integer type specification is SIGNED [INTEGER], so you have to say AS SIGNED or AS SIGNED INTEGER regardless of the type before conversion.

First of all, the result of COUNT(*) is an integer type, so CAST would not be necessary, so when I created SQL like this, it started to work with MySQL 5.6.The version of MySQL is not specified in your question, so I can't say for sure what will happen in your environment (it shouldn't work because it's through HeidiSQL), but please try it.

(By the way, this SQL didn't work with PostgreSQL 9.3, although the linked article states that LPAD is DBMS dependent, it seems difficult to write SQL that works with multiple DBMSs including subtle differences.)


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.