简体   繁体   English

If-then-else 逻辑决定运行哪个选择语句

[英]If-then-else logic to decide which select statement to run

I'm working on some scripting, and I'm trying to figure out how to add the proper logic so that depending on what a variable that is passed in is (I'll use x as an example), that one of two select statements will run.我正在编写一些脚本,我试图弄清楚如何添加正确的逻辑,以便根据传入的变量是什么(我将使用 x 作为示例),两个选择之一语句将运行。 Here's a simple example of what I'm looking for:这是我正在寻找的一个简单示例:

two select statements options X equals either "1" or "2"两个选择语句选项 X 等于“1”或“2”

if X equals 1 then run "select * from table where y=3 and z=4;"如果 X 等于 1,则运行“select * from table where y=3 and z=4;”

if X equals 2 then run "select * from table where y=3 and z=5 and q=6 and r=12;"如果 X 等于 2,则运行“select * from table where y=3 and z=5 and q=6 and r=12;”

The fact that the where clauses are different for the two statements is causing me issues since it makes it harder to do a CASE statement how I normally would.两个语句的 where 子句不同这一事实导致了我的问题,因为它使我更难像往常一样执行 CASE 语句。

The first thing that came to my mind was to do something that would do something like a case statement in the from clause so that I could do something like:我想到的第一件事就是在 from 子句中做一些类似于 case 语句的事情,这样我就可以做这样的事情:

    select * from 
    (
    case when x=1 then (select 1 from dual)
    case when x=2 then (select 2 from dual)
    )

But I know this doesn't work and I'm not sure of anyway to add this kind of logic to a from clause.但我知道这行不通,而且我不确定是否可以将这种逻辑添加到 from 子句中。

To summarize, I'm trying to run one of two possible queries depending on the value of a variable that is passed into the SQL, but the two queries have different where clauses that make it harder to have a case statement in the where clause.总而言之,我试图根据传递给 SQL 的变量的值运行两个可能的查询之一,但是这两个查询具有不同的 where 子句,这使得在 where 子句中使用 case 语句变得更加困难。 I'd appreciate any input or suggestions on how to tackle this issue.我很感激有关如何解决此问题的任何意见或建议。

Thanks in advanced guys!感谢先进的家伙!

Use simple OR :使用简单的OR

SELECT *
FROM your_table
WHERE (:X = 1 AND y=3 AND z=4)
   OR (:X = 2 AND y=3 and z=5 and q=6 and r=12);

My suggestion is not necessarily the most efficient way to do it especially in your case, because you have a relatively simple task in mind.我的建议不一定是最有效的方法,尤其是在您的情况下,因为您有一个相对简单的任务。 However, it is worth mentioning that for this case and even more complicated cases you can always write a procedure:但是,值得一提的是,对于这种情况,甚至更复杂的情况,您始终可以编写一个过程:

drop procedure if exists optionalClause;
CREATE PROCEDURE optionalClause()
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `optionalClause`()
begin
    DECLARE x INT DEFAULT 0;
    /* determining the value of x, for example using select into statement*/

    if @x=1 then
        /* select statement 1 */
    end if;

    if @x=2 then
        /* select statement 2 */
    end if;

END;
$$ DELIMITER ;
CALL optionalClause();

and of course you can have else statement instead of two separate if-statements and make it as complicated as you want.当然,您可以使用 else 语句而不是两个单独的 if 语句,并根据需要使其变得复杂。 Good Luck!祝你好运!

You could try something along these lines, replace the variable with the correct integer from your select statement.您可以尝试按照这些方式进行操作,用 select 语句中的正确整数替换变量。

declare @x int = 2

if @x = 1 goto No1
if @x = 2 goto No2

No1:

print ('This is x = 1')
select * from table where y=3 and z=4;

goto TheEnd

No2:

print ('This is x = 2')
select * from table where y=3 and z=5 and q=6 and r=12;

goto TheEnd

TheEnd:

Print('you made it')

CASE is going to give you IF-THEN logic within a SELECT statement. CASE 将在 SELECT 语句中为您提供 IF-THEN 逻辑。 For example:例如:

SELECT CASE WHEN x=1 THEN field1 ELSE field2 END AS [Conditional Field]
FROM tablename;

Alternatively, you can use IF-THEN-ELSE to run one statement or another.或者,您可以使用IF-THEN-ELSE来运行一个或另一个语句。 For example:例如:

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

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