fixed syntax bug in previous commit
fixed some bugs about sql query