Is there a way to create a query where as a user is prompted with a dialog to enter the value X to complete the following query:

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;
有帮助吗?

解决方案

Assuming you are coding an app wherein the user supplies the inputs, there are multiple ways to create a query that uses those values as variables - one way is as follows:

SET @t1=1, @t2=2, @t3:=4;
SELECT @t1, @t2;

Source: http://dev.mysql.com/doc/refman/5.5/en/user-variables.html

So for your particular case, replacing all the instances of X with the MySQL syntax for a user-defined variable @X, it would look something like this:

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top