Amysql select query building helper function