Factored out common code to generate 'where' stmt from key.

Also, fixed test failing for MySQL.