有没有办法创建一个查询,其中提示用户输入值X以完成以下查询:
SELECT X AS DISTANCE,
SUM(ABS(LOCX) <= X AND ABS(LOCY) <= X) AS QUANTITY,
COUNT(*) AS TOTAL,
CONCAT(AVG(ABS(LOCX) <= X AND ABS(LOCY) <= X)*100, '%') AS PERCENTAGE
FROM CUSTOMER;
答案 0 :(得分:0)
假设您正在编写用户提供输入的应用程序,有多种方法可以创建将这些值用作变量的查询 - 一种方法如下:
SET @t1=1, @t2=2, @t3:=4;
SELECT @t1, @t2;
来源:http://dev.mysql.com/doc/refman/5.5/en/user-variables.html
因此,对于您的特定情况,将X
的所有实例替换为用户定义变量@X
的MySQL语法,它看起来像这样:
SET @X = user_input;
SELECT @X AS DISTANCE,
SUM(ABS(LOCX) <= @X AND ABS(LOCY) <= @X) AS QUANTITY,
COUNT(*) AS TOTAL,
CONCAT(AVG(ABS(LOCX) <= @X AND ABS(LOCY) <= @X)*100, '%') AS PERCENTAGE
FROM CUSTOMER;