在MySQL中评估嵌套IF

时间:2013-11-26 17:59:53

标签: mysql

我在WordPress插件中找到了这段代码,但我无法理解它是什么意思或者如何阅读。

有人可以帮助我破坏这段代码:?

IF(agr_sam_ads.ad_users = 0, TRUE, IF(agr_sam_ads.ad_users_reg = 1, IF(agr_sam_ads.x_ad_users = 1, NOT FIND_IN_SET("admin", agr_sam_ads.x_view_users), TRUE ....

我没有粘贴整个查询,因为它很大。我有兴趣得到帮助的是查询的一部分,如下所示:

IF(expression, value, IF(expression, IF(expression, SQL Logical Query, value ...

我没有看到这种语法,我不知道在哪里搜索它。在MySQL文档中,IF语句的语法是这样的:http://dev.mysql.com/doc/refman/5.0/en/if.html,它看起来不像我上面粘贴的语法。

仅供用户对完整代码感兴趣,代码在此处:

SELECT 
agr_sam_places.id, 
agr_sam_places.name, 
agr_sam_places.description, 
agr_sam_places.code_before, 
agr_sam_places.code_after, 
agr_sam_places.place_size, 
agr_sam_places.place_custom_width, 
agr_sam_places.place_custom_height, 
agr_sam_places.patch_img, 
agr_sam_places.patch_link, 
agr_sam_places.patch_code, 
agr_sam_places.patch_adserver, 
agr_sam_places.patch_dfp, 
agr_sam_places.patch_source, 
agr_sam_places.trash, 
(
    SELECT 
        COUNT(*) 
    FROM 
        agr_sam_ads 
    WHERE 
        agr_sam_ads.pid = agr_sam_places.id 
    AND 
        agr_sam_ads.trash IS FALSE
) AS ad_count, 
(
    SELECT 
        COUNT(*) 
    FROM 
        agr_sam_ads 
    WHERE 
        agr_sam_ads.pid = agr_sam_places.id 
    AND 
        agr_sam_ads.trash IS FALSE 
    AND 
        (
            IF(agr_sam_ads.ad_users = 0, TRUE, IF(agr_sam_ads.ad_users_reg = 1, IF(agr_sam_ads.x_ad_users = 1, NOT FIND_IN_SET("admin", agr_sam_ads.x_view_users), TRUE
        ) 
    AND 
        IF(agr_sam_ads.ad_users_adv = 1, (agr_sam_ads.adv_nick <> "admin"), TRUE), FALSE))) 
    AND 
        (
            (
                agr_sam_ads.view_type = 1
            ) 
        OR 
            (
                agr_sam_ads.view_type = 0 
            AND 
                (
                    agr_sam_ads.view_pages+0 & 256
                )
            )
        ) 
    AND 
        (
            agr_sam_ads.ad_cats = 0
        ) 
    AND 
        (
            agr_sam_ads.ad_authors = 0
        ) 
    AND 
        IF(agr_sam_ads.ad_schedule, CURDATE() BETWEEN agr_sam_ads.ad_start_date AND agr_sam_ads.ad_end_date, TRUE) 
    AND 
        IF(agr_sam_ads.limit_hits, agr_sam_ads.hits_limit > agr_sam_ads.ad_hits, TRUE) 
    AND 
        IF(agr_sam_ads.limit_clicks, agr_sam_ads.clicks_limit > agr_sam_ads.ad_clicks, TRUE) 
    AND 
        (
            agr_sam_ads.ad_weight > 0
        )
) AS ad_logic_count, 
(
    SELECT 
        COUNT(*) 
    FROM 
        agr_sam_ads 
    WHERE 
        agr_sam_ads.pid = agr_sam_places.id 
    AND 
        agr_sam_ads.trash IS FALSE 
    AND 
        (
            IF(agr_sam_ads.ad_users = 0, TRUE, IF(agr_sam_ads.ad_users_reg = 1, IF(agr_sam_ads.x_ad_users = 1, NOT FIND_IN_SET("admin", agr_sam_ads.x_view_users), TRUE) 
        AND 
            IF(agr_sam_ads.ad_users_adv = 1, (agr_sam_ads.adv_nick <> "admin"), TRUE), FALSE))) 
        AND 
            (
                (
                    agr_sam_ads.view_type = 1
                ) 
            OR 
                (
                    agr_sam_ads.view_type = 0 
                AND 
                    (
                        agr_sam_ads.view_pages+0 & 256
                    )
                )
            ) 
        AND 
            (
                agr_sam_ads.ad_cats = 0
            ) 
        AND 
            (
                agr_sam_ads.ad_authors = 0
            ) 
        AND 
            IF(agr_sam_ads.ad_schedule, CURDATE() BETWEEN agr_sam_ads.ad_start_date AND agr_sam_ads.ad_end_date, TRUE) 
        AND 
            IF(agr_sam_ads.limit_hits, agr_sam_ads.hits_limit > agr_sam_ads.ad_hits, TRUE) 
        AND 
            IF(agr_sam_ads.limit_clicks, agr_sam_ads.clicks_limit > agr_sam_ads.ad_clicks, TRUE) 
        AND 
            IF(agr_sam_ads.ad_weight > 0, (agr_sam_ads.ad_weight_hits*10/(agr_sam_ads.ad_weight*1000)) < 1, FALSE)
    ) AS ad_full_count 
FROM 
    agr_sam_places 
WHERE 
    agr_sam_places.id = 10 
AND 
    agr_sam_places.trash IS FALSE;

2 个答案:

答案 0 :(得分:1)

应该理解为:

IF (
    expression, 
    valueIfExpressionIsTrue, 
    valueIfExpressionIsValue
)

所以要参与你的代码:

IF(
    agr_sam_ads.ad_users = 0,
    TRUE,
    IF(
        agr_sam_ads.ad_users_reg = 1,
        2,
        3
    )
)

我看到你熟悉PHP,所以基本上如果这是PHP,代码就是

if ($arg_sam_ads_ad_users == 0) {
    return true;
} else {
    if ($arg_sam_ads_ad_users_reg == 1) {
        return 2;
    } else {
        return 3;
    }
}

答案 1 :(得分:1)

这基本上是在不使用IF _ THEN _ ELSE _ ENDIF语法的情况下创建IF-ELSE树。

采用以下示例:

IF(1=0, 1, IF(1=1, 1, 0))

这与C语法相当:

IF (1=0) {
  1
}
ELSE IF (1=1) {
  1
}
ELSE {
  0
}

更常见的SQL语法如下:

IF 1=0 THEN
  1;
ELSEIF 1=1 THEN
  1;
ELSE
  0;
ENDIF;