fixed bugs with cases in mysql statements