MySQL 语法错误消息“操作数应包含 1 列”

发布于 2024-07-11 21:34:24 字数 774 浏览 4 评论 0原文

我尝试运行以下语句:

INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION)
SELECT (a.number, b.ID, b.DENOMINATION) 
FROM temp_cheques a, BOOK b
WHERE a.number BETWEEN b.START_NUMBER AND b.START_NUMBER+b.UNITS-1;

据我了解,应将 temp_cheques 中的每条记录插入到 VOUCHER 中,其中 ID 和 DENOMINATION 字段对应于 BOOK 表中的条目(temp_cheques 来自数据库备份,我正在尝试重新创建该备份)以不同的格式)。 然而,当我运行它时,我收到一个错误:

Error: Operand should contain 1 column(s)
SQLState:  21000
ErrorCode: 1241

我在 SQuirrel 中运行它,并且没有遇到任何其他查询的问题。 我的查询语法有问题吗?

编辑:

BOOK 的结构是:

ID  int(11)
START_NUMBER    int(11)
UNITS   int(11)
DENOMINATION    double(5,2)

temp_cheques 的结构是:

ID  int(11)
number  varchar(20)

I tried running the following statement:

INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION)
SELECT (a.number, b.ID, b.DENOMINATION) 
FROM temp_cheques a, BOOK b
WHERE a.number BETWEEN b.START_NUMBER AND b.START_NUMBER+b.UNITS-1;

which, as I understand it, should insert into VOUCHER each record from temp_cheques with the ID and DENOMINATION fields corresponding to entries in the BOOK table (temp_cheques comes from a database backup, which I'm trying to recreate in a different format). However, when I run it, I get an error:

Error: Operand should contain 1 column(s)
SQLState:  21000
ErrorCode: 1241

I'm running this in SQuirrel and have not had issues with any other queries. Is there something wrong with the syntax of my query?

EDIT:

The structure of BOOK is:

ID  int(11)
START_NUMBER    int(11)
UNITS   int(11)
DENOMINATION    double(5,2)

The structure of temp_cheques is:

ID  int(11)
number  varchar(20)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゃ懵逼小萝莉 2024-07-18 21:34:25

我手边没有 MySQL 实例,但我的第一个猜测是 WHERE 子句:

WHERE a.number BETWEEN b.START_NUMBER AND b.START_NUMBER+b.UNITS-1;

我想 MySQL 解析器可能会将其解释为:

WHERE number
(BETWEEN start_number AND start_number) + units - 1

尝试将所有内容都放在括号中,即:

WHERE a.number BETWEEN b.START_NUMBER AND (b.START_NUMBER + b.UNITS - 1);

I don't have a MySQL instance handy, but my first guess is the WHERE clause:

WHERE a.number BETWEEN b.START_NUMBER AND b.START_NUMBER+b.UNITS-1;

I imagine that the MySQL parser may be interpreting that as:

WHERE number
(BETWEEN start_number AND start_number) + units - 1

Try wrapping everything in parentheses, ie:

WHERE a.number BETWEEN b.START_NUMBER AND (b.START_NUMBER + b.UNITS - 1);
情释 2024-07-18 21:34:25

查询的最终版本如下:

Set SQL_BIG_SELECTS = 1;
INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION)
SELECT a.number, b.ID, b.DENOMINATION
FROM temp_cheques a, BOOK b
WHERE a.number BETWEEN b.START_NUMBER AND (b.START_NUMBER+b.UNITS-1);

BETWEEN 语句的解析需要括号,而 SELECT 不需要,并且由于两个表的大小(temp_cheques 中 215000 条记录,BOOK 中 8000 条记录),我打破了对选择大小,要求我设置 SQL_BIG_SELECTS = 1。

The final version of the query is as follows:

Set SQL_BIG_SELECTS = 1;
INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION)
SELECT a.number, b.ID, b.DENOMINATION
FROM temp_cheques a, BOOK b
WHERE a.number BETWEEN b.START_NUMBER AND (b.START_NUMBER+b.UNITS-1);

The parsing of the BETWEEN statement required parentheses, the SELECT did not, and because of the size of the two tables (215000 records in temp_cheques, 8000 in BOOK) I was breaking a limit on the select size, requiring me to set SQL_BIG_SELECTS = 1.

半山落雨半山空 2024-07-18 21:34:25

我在使用 Spring 存储库时遇到了同样的错误。

我的存储库包含如下方法:

List<SomeEntity> findAllBySomeId(List<String> ids);

在针对内存数据库(h2)运行集成测试时,该方法工作正常。 然而,对于像 MySql 这样的独立数据库来说,失败并出现相同的错误。

我通过将方法接口更改为:

List<someEntity findBySomeIdIn(List<String> ids);

注意:findfindAll 之间没有区别。 如下所述: Spring Data JPA findBy / findAllBy 之间的区别

I ran into the same error when using Spring Repositories.

My repository contained a method like:

List<SomeEntity> findAllBySomeId(List<String> ids);

This is working fine when running integration tests against an in-memory database (h2). However against a stand alone database like MySql is was failing with the same error.

I've solved it by changing the method interface to:

List<someEntity findBySomeIdIn(List<String> ids);

Note: there is no difference between find and findAll. As described here: Spring Data JPA difference between findBy / findAllBy

弥繁 2024-07-18 21:34:24

尝试从 SELECT 子句中删除括号。 来自 Microsoft TechNet,使用 SELECT 子句的 INSERT 语句的正确语法如下。

INSERT INTO MyTable  (PriKey, Description)
       SELECT ForeignKey, Description
       FROM SomeView

您收到的错误“SELECT 将检查超过 MAX_JOIN_SIZE 行;检查您的 WHERE 并使用 SET SQL_BIG_SELECTS=1 或 SET SQL_MAX_JOIN_SIZE=# 如果 SELECT 没问题。”实际上是正确的,假设您在两者中都有很多行预订和临时支票。 您尝试查询两个表中的所有行并进行交叉引用,从而产生 m*n 大小的查询。 SQL Server 会在执行可能较长的操作之前尝试警告您这一点。

在运行此语句之前设置 SQL_BIG_SELECTS = 1,然后重试。 它应该可以工作,但请注意此操作可能需要很长时间。

Try removing the parenthesis from the SELECT clause. From Microsoft TechNet, the correct syntax for an INSERT statement using a SELECT clause is the following.

INSERT INTO MyTable  (PriKey, Description)
       SELECT ForeignKey, Description
       FROM SomeView

The error you're getting, "The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay.", is actually correct, assuming you have many rows in both BOOK and temp_cheques. You are trying to query all rows from both tables and make a cross-reference, resulting in an m*n size query. SQL Server is trying to warn you of this, before performing a potentially long operation.

Set SQL_BIG_SELECTS = 1 before running this statement, and try again. It should work, but note that this operation may take a long time.

陌伤ぢ 2024-07-18 21:34:24

B 是否包含 UNITS 列?

temp_cheques 和 Book 的表结构是什么?

编辑:正如我在评论中所说,在进行 +/- 和比较时,所有列都应该是数字。
以下简单的 SELECT 有效吗?

<代码>
从书籍 B 中选择 b.START_NUMBER+b.UNITS-1

Does B contain the UNITS column?

What is the table structure for temp_cheques and Book?

EDIT: As I said in comments, all the columns should be numeric when doing +/- and when comparing.
Does the following simple SELECT work?


SELECT b.START_NUMBER+b.UNITS-1 FROM Books B

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文