Correct an SQL syntax error introduced in r30158. Adds tests.

Fixes #30339
See #18962
Props julien731

Built from https://develop.svn.wordpress.org/trunk@30480


git-svn-id: http://core.svn.wordpress.org/trunk@30468 1a063a9b-81f0-0310-95a4-ce76da25c4cd
WordPress 4.1-beta2

Built from https://develop.svn.wordpress.org/trunk@30481


git-svn-id: http://core.svn.wordpress.org/trunk@30469 1a063a9b-81f0-0310-95a4-ce76da25c4cd