Question

Goal: I'm trying to store the tables results of a query as a variable in a function. I'd like to be able to do more things in this procedure referencing these tables.

I've got a scaffold function below:

DELIMITER $$
CREATE FUNCTION getRatio(treatmentA INT, treatmentB INT)
RETURNS FLOAT
BEGIN
    DECLARE answer FLOAT;
    DECLARE countA, countB, countABIntersection;

    # Store patients of treatment A
    SELECT DISTINCT patient
    INTO **Variable**
    FROM Treatment
    WHERE TreatmentID = treatmentA;

    # Store patients of treatment B
    SELECT DISTINCT patient
    INTO **Variable**
    FROM Treatment
    WHERE TreatmentID = treatmentB;

    # Get Intersection of treatment B

    Set answer = count(TreatmentA) * count(TreatmentB) * count(IntersectionTreatmentAB)

    RETURN answer;
END $$
DELIMITER ;

If anyone can help, it would be greatly appreciated. Thanks in advance.

Était-ce utile?

La solution

You only can store or return a value from a function which MySQL have datatypes.

In short if you want to store a result set to variable you have to have a table datatype, unfortunately MySQL doesn't have such.

Solution: What you can do is, you can create temporary tables with respective columns for each result set to store(or may be in the same temp table in your case).

Do all operations on the temp table and store the result to a variable(FLOAT in you case), return the variable.

Let me know if that helps :)

-Thanks

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top