[英]IF -condition ERROR 1241 (21000): Operand should contain 1 column(s)
How to fix ? 怎么修 ?
mysql> SELECT IF(1,(SELECT * FROM zd LIMIT 1 ) ,(SELECT * FROM zd LIMIT 3) );
ERROR 1241 (21000): Operand should contain 1 column(s)
mysql>
Thank you. 谢谢。
The error code is more informative than it may seem at first. 错误代码比起初看起来更具信息性。
Operand should contain 1 columns
In your example, IF
is the operator , while 1
, (SELECT * FROM zd LIMIT 1 )
, and (SELECT * FROM zd LIMIT 3)
are the operands . 在您的示例中,
IF
是运算符 ,而1
, (SELECT * FROM zd LIMIT 1 )
和(SELECT * FROM zd LIMIT 3)
是操作数 。
The IF statement takes three parameters: a boolean value, a scalar value to return if the boolean is true, and a scalar value to return if the boolean is false. IF语句有三个参数:一个布尔值, 标值返回如果布尔是真实的,标值返回如果布尔是假的。
Your select subqueries each return a row. 您选择的子查询每个都返回一行。 You must return only a single value, like this:
您必须仅返回一个值,如下所示:
SELECT IF(1,(SELECT col1_name FROM zd LIMIT 1 ) ,(SELECT col2_name FROM zd LIMIT 1) )
where you replace col1_name
and col2_name
with whichever columns you want to get data from. 在其中用
col1_name
中获取数据的任何列替换col1_name
和col2_name
。
==== ====
Now that I read your question again, I see that what you're actually trying to do is conditionalize the limit. 现在,我再次阅读了您的问题,我发现您实际上想做的是限制条件。 I would just do that in PHP or whatever language you're querying the database with, if that's possible:
如果可能的话,我将使用PHP或您用来查询数据库的任何语言来执行此操作:
$limit = $bool ? 1 : 3;
$mysql->prepare("SELECT * FROM zd LIMIT $limit");
$mysql->execute();
$result = $mysql->fetch(PDO::FETCH_ASSOC)
I'm not sure there's a way to conditionally set the limit within mysql without variables. 我不确定有没有办法在没有变量的情况下在mysql中有条件地设置限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.