当 MySQL 基准测试返回多行时,如何解决该错误?
每当您在基准测试中返回多行时,都会出现错误。返回标量值或单行,而不是多行。语法如下所示 -
SELECT yourColumnName FROM yourTableName WHERE yourCondition.
为了理解上述语法,让我们创建一个表。创建表的查询如下所示 -
mysql> create table UserDemo -> ( -> UserId int, -> UserName varchar(20), -> RegisteredCourse varchar(10) -> ); Query OK, 0 rows affected (0.67 sec)
使用 insert 命令在表中插入一些记录。查询如下所示 -
mysql> insert into UserDemo values(1,'John','Java'); Query OK, 1 row affected (0.16 sec) mysql> insert into UserDemo values(2,'Larry','C'); Query OK, 1 row affected (0.21 sec) mysql> insert into UserDemo values(3,'Carol','C++'); Query OK, 1 row affected (0.11 sec) mysql> insert into UserDemo values(4,'Mike','C#'); Query OK, 1 row affected (0.17 sec)
使用 select 语句从表中显示所有记录。查询如下所示 -
mysql> select *from UserDemo;
以下是输出 -
+--------+----------+------------------+ | UserId | UserName | RegisteredCourse | +--------+----------+------------------+ | 1 | John | Java | | 2 | Larry | C | | 3 | Carol | C++ | | 4 | Mike | C# | +--------+----------+------------------+ 4 rows in set (0.00 sec)
您的基准错误如下所示 -
mysql> select benchmark(1000,(select RegisteredCourse from UserDemo)); ERROR 1242 (21000): Subquery returns more than 1 row
由于子查询返回多于一行,因此您需要返回一行。查询如下所示 -
mysql> select RegisteredCourse from UserDemo where UserId=1;
以下是输出 -
+------------------+ | RegisteredCourse | +------------------+ | Java | +------------------+ 1 row in set (0.00 sec)
广告