简体   繁体   English

IF语句中的MYSQL SELECT

[英]MYSQL SELECT WITHIN IF Statement

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. 你如何使用MYSQL IF语句。 No matter what I put in it doesn't work. 无论我放入什么都行不通。 In SQL Server this is a 5 second job. 在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. 谢谢你的帮助。

How to use IF statement in select 如何在select中使用IF语句

select if(status>0, 'active', 'inactive') as status from users

Yes, you can use select within IF 是的,您可以在IF使用select

Example: 例:

select if((select count(*) from versions) > (select count(*) from groups), true, false) as value

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;

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. 您可以从同一查询中选择另一个字段,但可能不支持使用select查询,因为查询可以返回多个值,mysql不支持列中的行概念。

SELECT IF(`field1`>1,`field2`,`field3`) FROM TABLE1 WHERE 1

here field1 , field2 , field3 are fields in a same table 这里field1field2field3是同一个表中的字段

Does this help you ? 这对你有帮助吗?

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 statement outside of the body of a stored procedure. 在MySQL中,您不能在存储过程的主体之外使用IF语句 The documentation implies this when it introduces the concept as "the IF statement for stored programs " (my emphasis). 当文档将概念引入“ 存储程序的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 (eg from PHP), except by querying values, then switching control flow based on those values outside of MySQL (ie using PHP's if statement instead of MySQL's). 据我所知,除了通过查询值,然后根据MySQL之外的那些值切换控制流,没有简单的方法来控制在命令行提示符或通过客户端(例如从PHP)发出的语句中的执行流程。 (即使用PHP的if语句而不是MySQL的)。 I would love to be corrected on this though :-) 我很乐意在此纠正:-)


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 ) (根据这个答案

you are looking for CASE WHEN ? 你正在寻找CASE WHEN吗? http://dev.mysql.com/doc/refman/5.0/en/case-statement.html 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. 我猜@Nanne链接更相关。 Just adding it to the list of links here. 只需将其添加到此处的链接列表中即可。 http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html

MySQL several queries using if illustrates an approach that is probably relevant here. 使用if的MySQL几个查询说明了一种可能与此相关的方法。

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?" 请注意,我假设这个问题是“如何使用表中的数据来引导控制流,这可能会改变数据库的其他部分?”

    CASE
        WHEN receiver = '$userid' THEN receiver
        ELSE sender
    END AS contact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM