3

以下是我所拥有的

+++++++++++++++
+ id + myData +
+++++++++++++++
+ 1  + ABCD   +
+ 2  + ABCDD  +
+++++++++++++++

当我执行

select char_length((select myData from mytable where id = 1)) as MyLength;

我得到的结果为

++++++++++++
+ MyLength +
++++++++++++
+  4       +
++++++++++++

但是当我执行查询时,

select char_length((select myData from mytable) as MyLength;

我得到错误

DDL and DML statements are not allowed in the query panel for MySQL; only SELECT statements are allowed.

演示

4

2 回答 2

3

您将整个查询输出包装在CHAR_LENGTH(). 将 应用于WHERE id = 1子查询时,它返回单行,并且由于您选择了单列,因此您将返回一个字符串值,该字符串值CHAR_LENGTH()将作为其参数接受。

如果子查询select mySata from mytable返回多行,char_length()则不能接受它作为输入。相反,只需在常规语句中应用CHAR_LENGTH()to即可查看多行的长度。myDataSELECT

SELECT CHAR_LENGTH(myData) AS myLength FROM mytable
于 2012-06-22T19:19:44.997 回答
0

随着char_length((select myData from mytable))您将行集对象传递给函数,这char_length是无法处理的。

When calling functions with query results, make sure you select only one row, and only one column, so the result can be interpreted as a single value.

于 2012-06-22T20:12:26.233 回答