Query failed: Table 'p' from one of the SELECTs cannot be used in field list