解决 MySQL Benchmark 中返回多行时的错误?
mysqlmysqli database
在基准测试中,只要返回多行,就会出错。返回标量值或单行,而不是多行。语法如下 −
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)