There are tables:
1. shop_order (id, contact_id,state_id) 2. contact_data (id, contact_id, field, value)
The second table is as follows:
id | contact_id | field | value 1 | 25 | phone | 89167777777 2 | 25 | city | Москва 3 | 26 | phone | 89164444444 4 | 26 | city | Москва 5 | 27 | phone | 89163333333 6 | 27 | city | Тверь
Table shop_order
id | contact_id | state_id 1 | 25 | paid 2 | 26 | paid 3 | 27 | notpaid
How to upload all orders (shop_order) and telephone numbers of the contact to these orders (contact_data), which have shop_order.state_id = 'paid', and the city of Moscow?