0

我需要使用 cakephp 和 mysql 将字符串传递给存储过程。

这是我传递给存储过程的字符串,

PlantingBlock.id IN (13,10)

下面是我的存储过程,

SET whereClause = " AND 1 = 1 ";
IF(in_clause IS NULL OR in_clause = '') THEN
    SET whereClause = CONCAT(whereClause, ' AND ',in_clause);
END IF;

SET statement = 'SELECT PlantingBlock.* FROM (
    SELECT PB.*, 
    B.block,
    V.variety,
    ST.size_type,
    PLA.date AS plant_date,
    PLA.week_no,
    C.crop,
    C.id AS crop_id
    FROM lf_planting_blocks AS PB,
         lf_blocks AS B,
         lf_properties AS P,
         lf_property_types AS PT,  
         lf_size_types AS ST,
         lf_varieties AS V,
         lf_plantings AS PLA,
         lf_crops AS C
    WHERE
        PB.block_id = B.id
        AND B.property_id = P.id
        AND P.property_type_id = PT.id
        AND B.size_type_id = ST.id
        AND PB.variety_id = V.id
        AND V.crop_id = C.id
        AND PB.planting_id = PLA.id
        AND PB.plant_still_active = true
        AND B.is_deleted = false
        AND P.is_deleted = false
        AND PT.is_deleted = false
        AND ST.is_deleted = false
        AND V.is_deleted = false
        AND C.is_deleted = false';

SET statement = CONCAT(statement, whereClause, ') AS PlantingBlock ORDER BY PlantingBlock.plant_date DESC');

这给出了一个名为 SQLSTATE[42S22] 的错误:未找到列:1054 Unknown column 'PlantingBlock.id' in 'field list'

4

1 回答 1

0

id问题是您尝试在使用赋予此子查询的别名引用列时向内部选择添加条件。

因此,您可以使用括号关闭子查询并将WHERE子句应用于您的外部SELECT

SELECT PlantingBlock.* 
FROM 
(
   ...
) AS PlantingBlock
 WHERE PlantingBlock.id IN (13,10) 
 ORDER BY PlantingBlock.plant_date DESC

或者更好的是,只需在您的内部选择中引用 id 字段。

SELECT PlantingBlock.* 
FROM 
(
   ...
   AND PB.id IN (13,10)
) AS PlantingBlock
 ORDER BY PlantingBlock.plant_date DESC
于 2013-08-28T04:19:12.347 回答