Fixing sql query problem