Fix table linkage in query, and duplicated conditions :(