Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql Function - not sure where to start
    primarykey
    data
    text
    <p>I would like to write a function in Mysql that from a given product code return a formatted string</p> <p>Here is an example of the calls I need to manually make now to get the result I want.</p> <pre><code>SELECT p.productcategoryid from products p where (isnull(p.endeffdt) or (p.endeffdt = '0000-00-00') or (p.endeffdt &gt; now())) and p.code='T29R66N1'; </code></pre> <p>T29R66NQ is the product code I need the full path for - the above call returns '38' as the category ID.</p> <p>I then perform the following select based on the result from above</p> <pre><code>SELECT name,parentid,productcategorypath FROM productcategory WHERE recid = '38'; </code></pre> <p>This returns</p> <p>name->Built-In Hobs<br> parentid->7<br> productcategorypath=222,7,38</p> <p>Using that result I then</p> <pre><code>SELECT name,parentid,productcategorypath FROM productcategory WHERE recid = '7'; </code></pre> <p>giving me</p> <p>name->Built-In<br> parentid->222<br> productcategorypath=222,7</p> <p>and again, I then do</p> <pre><code>SELECT name,parentid,productcategorypath FROM productcategory WHERE recid = '222'; </code></pre> <p>which in turn gives me</p> <p>name->Kitchen &amp; Home Appliances<br> parentid->0<br> productcategorypath=222</p> <p>I stop there because parentid = 0 (it may go on for more iterations but will always end with parent id of 0) but i need the results from the last 3 selects to give me the following string</p> <pre><code>Kitchen &amp; Home Appliances &gt; Built-In &gt; Built-In Hobs </code></pre> <p>I would like a mysql function whereby I can use it like</p> <pre><code>select getpath(code) from products where code='T29R66N1' </code></pre> <p>Any help would be appreciated.</p> <p>EDIT:</p> <p>I managed to figure it myself - here is my function</p> <blockquote> <p>DROP FUNCTION IF EXISTS mydb.getpath;</p> <p>CREATE FUNCTION mydb.getpath (itemid VARCHAR(20))</p> <p>RETURNS varchar(255) </p> <p>BEGIN</p> <p>DECLARE path_name varchar(255);<br> DECLARE tmp_name varchar(255);<br> DECLARE tmp_parentid INT;<br> DECLARE tmp_parentid1 INT;</p> <p>SELECT p.productcategoryid INTO tmp_parentid from products p where (isnull(p.endeffdt) or (p.endeffdt = '0000-00-00') or (p.endeffdt > now())) and p.code=itemid;</p> <p>myloop:LOOP<br></p> <blockquote> <p>SELECT name,parentid INTO tmp_name,tmp_parentid1 FROM productcategory WHERE recid = tmp_parentid;<br> SET path_name = concat_ws(' > ', tmp_name,path_name);<br> IF tmp_parentid1!=0 THEN<br> SET tmp_parentid = tmp_parentid1;<br> ITERATE myloop;<br> ELSE<br> LEAVE myloop;<br> END IF; <br> END LOOP;<br></p> </blockquote> <p>RETURN path_name;<br></p> <p>END;<br></p> </blockquote>
    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.
 

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