我所拥有的是一个位列NonMileage
,并且基于该位列,我想创建一个我可以在where子句中使用的变量。
这是一个两部分问题:
SET @NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT
@NoMileageListing CASE @NoMileageListing when 1 then
SET @MileListingClause = 'tblcargarage.miles >= 0' else
SET @MileListingClause = 'tblcargarage.miles != 0' end case;
这是答案
SET @NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT CASE @NoMileageListing
WHEN 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO @NoMileWhereClause;
select @NoMileWhereClause;
在这里找到: Mysql Storing a variable with the result of an SELECT CASE
答案 0 :(得分:1)
我认为问题出在时间部分。 它应该是:
SELECT CASE @NoMileageListing
WHEN @NoMileageListing = 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO @NoMileWhereClause;
select @NoMileWhereClause;
使用@noMileWhereClause创建动态查询请参考以下答案: How To have Dynamic SQL in MySQL Stored Procedure