{"id":51,"date":"2008-12-18T13:40:35","date_gmt":"2008-12-18T13:40:35","guid":{"rendered":"http:\/\/www.weeklywhinge.com\/?p=51"},"modified":"2010-05-06T15:33:20","modified_gmt":"2010-05-06T14:33:20","slug":"curses","status":"publish","type":"post","link":"https:\/\/www.weeklywhinge.com\/?p=51","title":{"rendered":"curses!"},"content":{"rendered":"<p>Say you want to step through a bunch of records in order using a MySQL cursor. That&#8217;s normally not a problem: you create a stored procedure so:<br \/>\n<code>CREATE PROCEDURE doit()<br \/>\nBEGIN<br \/>\n<span style=\"margin-left: 15px;\">  DECLARE whichi INT(11) DEFAULT 0;<\/span><br \/>\n<span style=\"margin-left: 15px;\">    DECLARE mycur CURSOR FOR SELECT IF(i=1, f1, f2) AS whichi ORDER BY whichi;<\/span><br \/>\n<span style=\"margin-left: 15px;\">    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET cursorExpired=true;<\/span><br \/>\n<span style=\"margin-left: 15px;\">    open  mycur; <\/span><br \/>\n<span style=\"margin-left: 15px;\">    curnext: loop <\/span><br \/>\n<span style=\"margin-left: 30px;\">    FETCH mycur INTO whichi; <\/span><br \/>\n<span style=\"margin-left: 30px;\">    IF cursorExpired THEN <\/span><br \/>\n<span style=\"margin-left: 45px;\">      LEAVE curnext;<\/span><br \/>\n<span style=\"margin-left: 30px;\">    END IF; <\/span><br \/>\n<span style=\"margin-left: 30px;\">    select whichi; <\/span><br \/>\n<span style=\"margin-left: 15px;\">  END LOOP curnext; <\/span><br \/>\n<span style=\"margin-left: 15px;\">  close mycur;<\/span><br \/>\nEND;<br \/>\n<\/code><\/p>\n<p>Obviously that&#8217;s an incredibly inefficient way to list records, but you get the idea.<\/p>\n<p>The point of the query is that the SELECT must be ordered by the result of IF(i=1, f1, f2); however calling doit() will not output what you expect (unless, of course, you&#8217;ve come across this problem before, in which case you&#8217;ll be expecting it :))<\/p>\n<p>What you get is no ordering of the data at all &#8211; the ORDER BY part of the statement is effectively ignored. Why? Because we&#8217;ve called the field in the SELECT query the same as the variable we declared above. So you end up with (essentially)<br \/>\n<code><br \/>\n  SELECT IF(i=1, f1, f2) AS whichi ORDER BY 0;<br \/>\n<\/code><br \/>\nI guess you can imagine how long it took me to figure that one out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Say you want to step through a bunch of records in order using a MySQL cursor. That&#8217;s normally not a problem: you create a stored procedure so: CREATE PROCEDURE doit() BEGIN DECLARE whichi INT(11) DEFAULT 0; DECLARE mycur CURSOR FOR SELECT IF(i=1, f1, f2) AS whichi ORDER BY whichi; DECLARE CONTINUE HANDLER FOR SQLSTATE &#8216;02000&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,2],"tags":[15],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-webdev","category-whinges","tag-mysql"],"_links":{"self":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":4,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weeklywhinge.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}