Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot set optional parameters in MySQL stored procedures.<br> You can however set optional parameters in a MySQL UDF. </p> <p>You do know that MySQL has an <a href="http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_avg" rel="nofollow noreferrer">AVG aggregate function</a>?</p> <p><strong>Workaround</strong> If you can face the ugliness of this workaround here's samplecode that uses a comma separated string with values as input and returns the average.</p> <pre><code>DELIMITER $$ CREATE FUNCTION MyAvg(valuestr varchar) RETURNS float BEGIN DECLARE output float; DECLARE arg_count integer; DECLARE str_length integer; DECLARE arg float; DECLARE i integer; SET output = NULL; SET i = LENGTH(valuestr); IF i &gt; 0 THEN BEGIN SET arg_count = 1; WHILE i &gt; 0 DO BEGIN IF MID(valuestr, i, 1) SET i = i - 1; END; END WHILE; /* calculate average */ SET output = 0; SET i = arg_count; WHILE i &gt; 0 DO BEGIN SET arg = SUBSTRING_INDEX( SUBSTRING_INDEX(valuestr, ',' , i) , ',', -1 ); SET output = output + arg; SET i = i - 1; END; END WHILE; SET output = output / arg_count; END; END IF; RETURN output; END $$ DELIMITER ; </code></pre> <p>Use concat_ws to feed the function.</p> <pre><code>SELECT MyAvg(CONCAT_WS(',',100,200,300,500)) AS test; </code></pre> <p>You can also <a href="http://dev.mysql.com/doc/refman/5.1/en/adding-functions.html" rel="nofollow noreferrer">write an UDF</a> in <a href="https://stackoverflow.com/questions/1914776/mysql-aggregate-udf-user-defined-function-in-c">C(++)</a> or <a href="https://stackoverflow.com/questions/5894258/creating-a-udf-for-mysql-in-delphi">Delphi/Lazarus</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload