MySQL错误1064:搜索查询未按预期工作

huangapple go评论160阅读模式
英文:

MySQL error 1064: search query not working as expected

问题

我正在尝试编写一个查询来搜索一个表,但是我得到了通用的1064 MySQL语法错误。我没有看到我的查询有什么问题...事实上,我有另一个完全相同的查询,它可以正常工作。

这些查询使用Go中的SQL驱动程序,所有的?将在查询运行之前转换为值。

这是我的错误信息:

> 错误1064:您的SQL语法有误;请检查与您的MySQL服务器版本相对应的手册,以获取正确的语法使用方法,位于第3行附近:'SELECT tax.*
FROM tax
JOIN user_tax
ON user_tax.tax_id = tax.id
WHERE u'

例如,这是一个正常工作的查询:

SELECT tax.*
FROM tax
JOIN user_tax
	ON user_tax.tax_id = tax.id
WHERE user_tax.user_id = ?
	AND tax.deleted_at IS NULL
	AND user_tax.deleted_at IS NULL

上述查询将找到给定用户的所有税务记录。

现在我正在尝试做完全相同的查询,只是现在它是一个"搜索"查询。它接收一个搜索字符串,并在税务表的namerate列中查找。这是它:

SET @searchString = '%?%';

SELECT tax.*
FROM tax
JOIN user_tax
	ON user_tax.tax_id = tax.id
WHERE user_tax.user_id = ?
	AND tax.deleted_at IS NULL
	AND user_tax.deleted_at IS NULL
	AND tax.name LIKE @searchString
		OR tax.rate LIKE @searchString

这是代码中的查询:

var taxes []Tax
err := r.db.Select(&taxes, `
	SET @query = '%?%';

	SELECT tax.*
	FROM tax
	JOIN user_tax
		ON user_tax.tax_id = tax.id
	WHERE user_tax.user_id = ?
		AND tax.deleted_at IS NULL
		AND user_tax.deleted_at IS NULL
		AND tax.name LIKE @query
			OR tax.rate LIKE @query
`, q, userId)
英文:

I am attempting to write a query for searching on a table, but I am getting the generic 1064 MySQL syntax error. I don't see anything wrong with my query... in fact, I have another query just like it that works fine.

The queries are using an SQL driver in Go, all of the ? will be converted to values before the query is ran.

This is my error:

> 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 tax.*
FROM tax
JOIN user_tax
ON user_tax.tax_id = tax.id
WHERE u' at line 3

For instance, this is the working query:

SELECT tax.*
FROM tax
JOIN user_tax
	ON user_tax.tax_id = tax.id
WHERE user_tax.user_id = ?
	AND tax.deleted_at IS NULL
	AND user_tax.deleted_at IS NULL

The query above will find all tax records for a given user.

Now I am trying to make the same exact query, except now it is a "search" query. It takes in a search string and looks for it on the name and rate column of the tax table. This is it:

SET @searchString = '%?%';

SELECT tax.*
FROM tax
JOIN user_tax
	ON user_tax.tax_id = tax.id
WHERE user_tax.user_id = ?
	AND tax.deleted_at IS NULL
	AND user_tax.deleted_at IS NULL
	AND tax.name LIKE @searchString
		OR tax.rate LIKE @searchString

This is the query in code:

var taxes []Tax
err := r.db.Select(&taxes, `
	SET @query = '%?%';

	SELECT tax.*
	FROM tax
	JOIN user_tax
		ON user_tax.tax_id = tax.id
	WHERE user_tax.user_id = ?
		AND tax.deleted_at IS NULL
		AND user_tax.deleted_at IS NULL
		AND tax.name LIKE @query
			OR tax.rate LIKE @query
`, q, userId)

答案1

得分: 2

如果你想避免多次绑定相同的值,你可以在一个交叉连接的子查询中绑定它一次,并重复使用该值:

SELECT tax.*
FROM tax
JOIN user_tax
    ON user_tax.tax_id = tax.id
CROSS JOIN (SELECT ? as q) sub
WHERE user_tax.user_id = ?
    AND tax.deleted_at IS NULL
    AND user_tax.deleted_at IS NULL
    AND (tax.name LIKE sub.q
         OR tax.rate LIKE sub.q)

现在将%s%作为第一个参数进行绑定。

英文:

If you want to avoid binding the same value multiple times, you can bind it in a cross joined subquery once and reuse the value:

SELECT tax.*
FROM tax
JOIN user_tax
    ON user_tax.tax_id = tax.id
CROSS JOIN (SELECT ? as q) sub
WHERE user_tax.user_id = ?
    AND tax.deleted_at IS NULL
    AND user_tax.deleted_at IS NULL
    AND (tax.name LIKE sub.q
         OR tax.rate LIKE sub.q)

Now just bind %s% as the first parameter.

答案2

得分: 1

我不明白为什么要使用SET @query并将其传递到查询中,当你可以使用绑定参数?来完成。

以下是解决方案(我对go不太熟悉,如果go代码有问题,请谅解):

var taxes []Tax
query := "%something%youre%looking%for%"
err := r.db.Select(&taxes, `
    SELECT tax.*
    FROM tax
    JOIN user_tax
        ON user_tax.tax_id = tax.id
    WHERE user_tax.user_id = ?
        AND tax.deleted_at IS NULL
        AND user_tax.deleted_at IS NULL
        AND (
              tax.name LIKE ? 
              OR 
              tax.rate LIKE ?
            )
`, userId, query, query)

或者尝试以下代码(我不确定是否有效):

var taxes []Tax
query := "something"
err := r.db.Select(&taxes, `
    SET @query = CONCAT('%', ?, '%');

    SELECT tax.*
    FROM tax
    JOIN user_tax
        ON user_tax.tax_id = tax.id
    WHERE user_tax.user_id = ?
        AND tax.deleted_at IS NULL
        AND user_tax.deleted_at IS NULL
        AND (
              tax.name LIKE @query
              OR 
              tax.rate LIKE @query
            )
`, query, userId)
英文:

I don't understand why to do SET @query and pass it inside of it when You can do it by using bind parameter using ?.

So here is solution (I'm not familiar with go so please sorry if something wrong with go code):

var taxes []Tax
query := "%something%youre%looking%for%"
err := r.db.Select(&taxes, `

    SELECT tax.*
    FROM tax
    JOIN user_tax
        ON user_tax.tax_id = tax.id
    WHERE user_tax.user_id = ?
        AND tax.deleted_at IS NULL
        AND user_tax.deleted_at IS NULL
        AND (
              tax.name LIKE ? 
              OR 
              tax.rate LIKE ?
            )

`, q, userId, query, query)

OR try this (I'm not sure if it will work) :

var taxes []Tax
query := "something"
err := r.db.Select(&taxes, `
    SET @query = CONCAT('%', ?, '%');

    SELECT tax.*
    FROM tax
    JOIN user_tax
        ON user_tax.tax_id = tax.id
    WHERE user_tax.user_id = ?
        AND tax.deleted_at IS NULL
        AND user_tax.deleted_at IS NULL
        AND (
              tax.name LIKE @query
              OR 
              tax.rate LIKE @query
            )

`, q, query, userId)

答案3

得分: 1

MySQL报告的语法错误包含魔术词**"check the manual that corresponds to your MySQL server version for the right syntax to use near '"**,后面跟着查询的尾部,该尾部与已解析并正确的前部不匹配。

当错误消息中报告的查询部分看起来不正确时,通常是在它之前缺少了某些内容。

在你的情况下,似乎没有缺少任何内容,这导致一个结论:你尝试运行两个查询(正确地用分号分隔),但是你用于向服务器发送查询的库不知道这一点,并且认为它只是一个查询。连接的另一侧也发生了同样的情况,MySQL服务器在第一个查询(及其终止的分号)之后发现的所有内容都报告为"语法错误"。

不要在单个调用中发送两个查询,或者使用允许在同一个字符串中包含多个查询的调用(如果库提供了这样的调用)。

英文:

The syntax errors reported by MySQL contain the magic words "check the manual that corresponds to your MySQL server version for the right syntax to use near '" followed by the trailing part of the query that doesn't fit with the leading part that was already parsed and found as correct.

When the part of the query reported in the error message doesn't look incorrect, something is usually missing just before it.

In your case it seems nothing is missing and this leads to a single conclusion: you try to run two queries (correctly separated by ;) but the library you use to send queries to the server doesn't know that and thinks it is only one query. The same happens on the other side of the connection and the MySQL server simply reports as "Syntax error" everything it founds after the first query (and its terminating ;).

Don't send two queries in a single call or use a call that allows multiple queries in the same string (if such a call is provided by the library).

huangapple
  • 本文由 发表于 2017年7月29日 19:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/45388963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定