문제

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