Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在不带子查询的where子句中使用用户变量_Mysql - Fatal编程技术网

Mysql 在不带子查询的where子句中使用用户变量

Mysql 在不带子查询的where子句中使用用户变量,mysql,Mysql,我有以下情况: +-----------+ | my_column | +-----------+ | A | | B | | C | | D | | E | +-----------+ 对于这个例子,我简化了下面的函数 DROP FUNCTION IF EXISTS my_function; CREATE FUNCTION my_function( phrase VARCHAR(255), co

我有以下情况:

+-----------+
| my_column |
+-----------+
| A         |
| B         |
| C         |
| D         |
| E         |
+-----------+
对于这个例子,我简化了下面的函数

DROP FUNCTION IF EXISTS my_function;
CREATE FUNCTION my_function(
    phrase VARCHAR(255), 
    column_value VARCHAR(255)
)
RETURNS FLOAT(20,10)
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
    IF(column_value = 'A') THEN RETURN 1.0000000000;
    ELSEIF(column_value = 'B') THEN RETURN 0.7500000000;
    ELSEIF(column_value = 'C') THEN RETURN 0.7500000000;
    ELSEIF(column_value = 'D') THEN RETURN 0.5000000000;
    ELSEIF(column_value = 'E') THEN RETURN 0.0000000000;
    END IF;
END;
以下是我的主要存储过程:

DROP PROCEDURE IF EXISTS my_procedure;
CREATE PROCEDURE my_procedure(  
    IN phrase VARCHAR(255)
)
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
    SET @phrase = phrase;
    SET @query = "
        SELECT  
            my_column, 
            @score_var := my_function(?,my_column) as score, 
            @score_var
        FROM my_table
        ORDER BY score DESC;
    ";
    PREPARE stmt FROM @query;
    EXECUTE stmt USING @phrase; 
    DEALLOCATE PREPARE stmt;
END;
现在如果我调用我的程序

称我的程序为“任何东西”

结果是:

+-----------+--------------+------------+
| my_column | score        | @score_var |
+-----------+--------------+------------+
| A         | 1.0000000000 |          1 |
| B         | 0.7500000000 |       0.75 |
| C         | 0.7500000000 |       0.75 |
| D         | 0.5000000000 |        0.5 |
| E         | 0.0000000000 |          0 |
+-----------+--------------+------------+
+-----------+--------------+------------+
| my_column | score        | @score_var |
+-----------+--------------+------------+
| A         | 1.0000000000 |          1 |
| C         | 0.7500000000 |       0.75 |
| E         | 0.0000000000 |          0 |
+-----------+--------------+------------+
但如果我在我的\u过程的查询中添加WHERE@score\u var>0.5,结果是:

+-----------+--------------+------------+
| my_column | score        | @score_var |
+-----------+--------------+------------+
| A         | 1.0000000000 |          1 |
| B         | 0.7500000000 |       0.75 |
| C         | 0.7500000000 |       0.75 |
| D         | 0.5000000000 |        0.5 |
| E         | 0.0000000000 |          0 |
+-----------+--------------+------------+
+-----------+--------------+------------+
| my_column | score        | @score_var |
+-----------+--------------+------------+
| A         | 1.0000000000 |          1 |
| C         | 0.7500000000 |       0.75 |
| E         | 0.0000000000 |          0 |
+-----------+--------------+------------+
预期结果´>0.5´:

+-----------+--------------+------------+
| my_column | score        | @score_var |
+-----------+--------------+------------+
| A         | 1.0000000000 |          1 |
| B         | 0.7500000000 |       0.75 |
| C         | 0.7500000000 |       0.75 |
+-----------+--------------+------------+
我见过一些使用子查询的答案,但我的问题是:在这种情况下,我可以不使用子查询吗


也欢迎使用其他方法。

当您在同一语句中读写用户变量时,该行为将被记录为未记录。换句话说,除非您阅读并理解MySQL版本的源代码,否则结果是不可预测的

然而,我认为你不必要地把事情复杂化了。我不认为有理由使用预先准备好的语句,也不认为有理由使用u用户变量。您的程序主体可以是:

SELECT  
    my_column, 
    my_function(phrase, my_column) as score, 
FROM my_table
HAVING score > 0.5
ORDER BY score DESC
此外,您的函数可以用较少的代码重复编写:

RETURN
    CASE column_value
        WHEN 'A' THEN 1.0000000000
        WHEN 'B' THEN 0.7500000000
        WHEN 'C' THEN 0.7500000000
        WHEN 'D' THEN 0.5000000000
        WHEN 'E' THEN 0.0000000000
    END
是的,先生。。。以我的尊敬回应。还感谢您提供了代码更少的示例