In a little app where users can create tabs and portlets in those tabs I ran into some issues with my ordered list implementation. It turns out I just misplaced a filtering expression in the WHERE clause instead of the JOIN condition. The basic idea was that I wanted to add new portlets in the left hand column at the top. The positions from the portlets actually start at 0, but what I am doing is to insert them at 1 below the current minimum or -1 if the table is empty. Then in a second query I push everything up by one. Seemed like the most efficient way to deal with concurrency. I guess in theory I could even skip pushing things up by one, but oh well.
Here is the original query (note that I am using PDO which supports Oracle style named placeholders for all drivers including MySQL):
INSERT INTO user_tab_portlets (tab_id, portlet_id, pos, col, is_open, config)
(SELECT :tab_id, :portlet_id, COALESCE(MIN(utp.pos)-1, …
[Read more]