Fix some database queries

In the hope non-mysql users might start to experience better things