MYSQL SELECT WITHIN IF 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4774837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MYSQL SELECT WITHIN IF Statement
提问by tura
This is such a simple question but I haven't found an answer anywhere for 2 hours.
这是一个如此简单的问题,但我已经 2 个小时没有在任何地方找到答案。
How do you use the MYSQL IF statment. No matter what I put in it doesn't work. In SQL Server this is a 5 second job.
你如何使用MYSQL IF 语句。不管我放什么都行不通。在 SQL Server 中,这是一个 5 秒的工作。
Does it need to be within a procedure?
它需要在一个程序内吗?
Can you use a SELECT within an IF statement?
您可以在 IF 语句中使用 SELECT 吗?
Not really interested in the IF function here.
对这里的 IF 函数不是很感兴趣。
Thanks for any help.
谢谢你的帮助。
回答by Srihari Goud
How to use IF
statement in select
如何IF
在select中使用语句
select if(status>0, 'active', 'inactive') as status from users
Yes, you can use select
within IF
是的,您可以select
在IF
Example:
例子:
select if((select count(*) from versions) > (select count(*) from groups), true, false) as value
回答by Benoit
Does it need to be within a procedure?
它需要在一个程序内吗?
Yes, you do need to be in a procedure to use an if statement.
是的,您确实需要在过程中才能使用 if 语句。
Can you use a SELECT within an IF statement?
您可以在 IF 语句中使用 SELECT 吗?
Yes you can:
是的你可以:
drop procedure if exists sp;
create procedure sp () begin
if ((select 1 /*from whatever table would work*/)=1) then
select 'it works';
end if;
end;
call sp;
drop procedure sp;
回答by Harish
You can select another field from the same query but using select query might not be supported as query can return multiple values , mysql not supporting row concept in a column.
您可以从同一查询中选择另一个字段,但可能不支持使用选择查询,因为查询可以返回多个值,mysql 不支持列中的行概念。
SELECT IF(`field1`>1,`field2`,`field3`) FROM TABLE1 WHERE 1
here field1
,field2
,field3
are fields in a same table
这里field1
, field2
,field3
是同一个表中的字段
Does this help you ?
这对你有帮助吗?
回答by Rich
You hinted at it in your comment, but none of the answers here state it explicitly, so I will:
您在评论中暗示了这一点,但这里的答案都没有明确说明,所以我会:
In MySQL, you cannot use an IF statementoutside of the body of a stored procedure. The documentation implies this when it introduces the concept as "the IF statement for stored programs" (my emphasis).
在 MySQL 中,您不能在存储过程主体之外使用IF 语句。文档在将概念引入为“存储程序的 IF 语句”(我的重点)时暗示了这一点。
As far as I can see, there is no easy way to control execution flow in statements issued at the command line prompt or via the client (e.g. from PHP), except by querying values, then switching control flow based on those values outside of MySQL (i.e. using PHP's if
statement instead of MySQL's). I would love to be corrected on this though :-)
据我所知,没有简单的方法来控制在命令行提示符下或通过客户端(例如从 PHP)发出的语句中的执行流,除非通过查询值,然后根据这些值在 MySQL 之外切换控制流(即使用 PHP 的if
语句而不是 MySQL 的语句)。我很想在这方面得到纠正:-)
You can simulate this on a case-by-case basis using other constructions. For example:
您可以使用其他结构逐案模拟这一点。例如:
IF x THEN
INSERT INTO mytable (mycol)
VALUES ('foo')
END IF
could become
可以成为
INSERT INTO mytable (mycol)
SELECT 'foo'
FROM dual
WHERE x
(as per this answer)
(根据这个答案)
You can also create, call and then drop a new stored procedure:
您还可以创建、调用然后删除一个新的存储过程:
DELIMITER \
CREATE PROCEDURE tmpfunc() BEGIN
IF x THEN
INSERT INTO mytable (mycol)
VALUES ('foo');
END IF;
END \
DELIMITER ;
CALL tmpfunc();
DROP PROCEDURE tmpfunc;
(as per this answer)
(根据这个答案)
回答by Nishant
you are looking for CASE WHEN
? http://dev.mysql.com/doc/refman/5.0/en/case-statement.html
你在找CASE WHEN
什么?http://dev.mysql.com/doc/refman/5.0/en/case-statement.html
IF
is already there in manual http://dev.mysql.com/doc/refman/5.0/en/if-statement.html
IF
手册中已经存在http://dev.mysql.com/doc/refman/5.0/en/if-statement.html
I guess @Nanne link is more relevant. Just adding it to the list of links here. http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
我猜@Nanne 链接更相关。只需将其添加到此处的链接列表中即可。http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html
回答by SuperSpy
CASE
WHEN receiver = '$userid' THEN receiver
ELSE sender
END AS contact
回答by rdm
MySQL several queries using ifillustrates an approach that is probably relevant here.
MySQL 使用 if 的几个查询说明了一种可能与此处相关的方法。
Note that I have assumed that this question is asking "how can I use data from a table to direct control flow which might alter some other part of the database?"
请注意,我假设这个问题是在问“我如何使用表中的数据来指导可能改变数据库其他部分的控制流?”