【问题标题】:MySQL : IS NOT NULL check on custom generated column(alias)MySQL:对自定义生成的列(别名)进行 IS NOT NULL 检查
【发布时间】:2015-07-24 06:25:06
【问题描述】:

存储过程到目前为止工作正常,但我想要记录仅在 dist_calculated IS NOT NULL 时。当我在 where clause 中使用该条件时,它显示错误 #1054 - Unknown column 'dist_calculated' in 'where clause'。如果没有 where 子句,它运行良好并且返回 NULL 记录也像:

entity_id   dist_calculated     
49              NULL
50              NULL
52              4460.615
51              4875.179

我想排除 NULL。

我试过WHERE dist_calculated IS NOT NULLWHERE cpe.dist_calculated IS NOT NULL 仍然报错。

我的存储过程是:

DELIMITER //
CREATE PROCEDURE get_close_childcares(IN latUser DECIMAL(15,6),IN lngUser DECIMAL(15,6) )
BEGIN
    /*Get 4 ids of closest childcares*/
    /*Outer query   
    @param : userLat, userLng, Current childcare lat,current childcare lng
    Note : Inner query returns lat , lng of Current product 176 : lat , 177: lng
    */
    SELECT cpe.entity_id , get_distance_in_miles_between_geo_locations(latUser,lngUser,
        (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
            WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 176)
        ),
        (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
            WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 177)
        )
    ) AS dist_calculated
    FROM catalog_product_entity AS cpe
    WHERE dist_calculated IS NOT NULL
    ORDER BY dist_calculated ASC
    LIMIT 0,4;

END
//
DELIMITER ;

并调用存储过程:

call get_close_childcares(19.992100,73.777000)

谢谢。

【问题讨论】:

    标签: mysql stored-procedures where alias notnull


    【解决方案1】:

    MySQL 确实允许在 GROUP BY 和 HAVING 上使用列别名,但不允许在 WHERE 语句中使用。因此,您需要在 WHERE 中使用完整的定义,例如

    SELECT cpe.entity_id , get_distance_in_miles_between_geo_locations(latUser,lngUser,
        (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
            WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 176)
        ),
        (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
            WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 177)
        )
    ) AS dist_calculated
    FROM catalog_product_entity AS cpe
    WHERE
    get_distance_in_miles_between_geo_locations(latUser,lngUser,
            (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
                WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 176)
            ),
            (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
                WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 177)
            )
        ) IS NOT NULL
    ORDER BY dist_calculated ASC
    LIMIT 0,4;
    

    【讨论】:

    • 你能告诉我what could be done here then 吗?
    【解决方案2】:

    你可以试试这个伙伴:

    SELECT
        cpe.entity_id,
        get_distance_in_miles_between_geo_locations (
            latUser,
            lngUser,
            cpev_1.`value`,
            cpev_2.`value`
        ) AS dist_calculated
    FROM
        catalog_product_entity cpe
        INNER JOIN catalog_product_entity_varchar cpev_1 ON cpev_1.entity_id = cpe.entity_id
            AND cpev_1.attribute_id = 176
        INNER JOIN catalog_product_entity_varchar cpev_2 ON cpev_2.entity_id = cpev.entity_id
            AND cpev_2.attribute_id = 177
    ORDER BY
        dist_calculated ASC
    LIMIT 0,4;  
    

    【讨论】:

      【解决方案3】:

      请尝试

      CREATE PROCEDURE get_close_childcares(IN latUser DECIMAL(15,6),IN lngUser DECIMAL(15,6) )
      BEGIN
          /*Get 4 ids of closest childcares*/
          /*Outer query   
          @param : userLat, userLng, Current childcare lat,current childcare lng
          Note : Inner query returns lat , lng of Current product 176 : lat , 177: lng
          */
          SELECT cpe.entity_id , get_distance_in_miles_between_geo_locations(latUser,lngUser,
              (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
                  WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 176)
              ),
              (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
                  WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 177)
              )
          ) AS dist_calculated
          FROM catalog_product_entity AS cpe
          WHERE get_distance_in_miles_between_geo_locations(latUser,lngUser,
              (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
                  WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 176)
              ),
              (SELECT cpev.value FROM catalog_product_entity_varchar AS cpev 
                  WHERE (cpev.entity_id = cpe.entity_id AND cpev.attribute_id = 177)
              )
          ) IS NOT NULL
          ORDER BY dist_calculated ASC
          LIMIT 0,4;
      
      END
      

      【讨论】:

      • 不工作,为其生成存储过程会出错
      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 2015-04-25
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 2011-11-04
      相关资源
      最近更新 更多