SELECT 
o.order_id orders_id,
lower(bt.user_email) customers_id, 
Concat(bt.first_name,' ', bt.last_name)  customers_name, 
bt.company customers_company, 
bt.address_1 customers_street_address, 
bt.address_2 customers_suburb, 
bt.city customers_city, 
bt.zip customers_postcode, 
bt.state customers_state, 
btcountry.country_name  customers_country, 
bt.phone_1 customers_telephone, 
bt.user_email customers_email_address, 
1 customers_address_format_id, 
Concat(del.first_name,' ', del.last_name) delivery_name, 
del.company delivery_company, 
del.address_1 delivery_street_address, 
del.address_2 delivery_suburb, 
del.city delivery_city, 
del.zip delivery_postcode, 
del.state delivery_state, 
delcountry.country_name delivery_country, 
1 delivery_address_format_id, 
Concat(bt.first_name,' ', bt.last_name) billing_name, 
bt.company billing_company, 
bt.address_1 billing_street_address, 
bt.address_2 billing_suburb, 
bt.city billing_city, 
bt.zip billing_postcode, 
bt.state billing_state, 
btcountry.country_name billing_country, 
1 billing_address_format_id, 
'' payment_method, 
1 cc_type, 
'' cc_owner, 
'' cc_number, 
'' cc_expires, 
from_unixtime(o.mdate) last_modified, 
from_unixtime(o.cdate) date_purchased, 
ost.order_status_id orders_status, 
from_unixtime(o.mdate) orders_date_finished, 
'EUR' currency, 
1 currency_value,
o.ship_method_id remark1,
pymntmt.payment_method_name remark2,
'' remark3
FROM <@PREFIX>vm_orders o
 join <@PREFIX>vm_user_info bt  
   on (o.user_id = bt.user_id 
   and bt.address_type ='BT')
left join <@PREFIX>vm_order_payment pymnt on (o.order_id = pymnt.order_id)
left join <@PREFIX>vm_payment_method pymntmt on (pymnt.payment_method_id = pymntmt.payment_method_id)
join <@PREFIX>vm_country btcountry on (bt.country = btcountry.country_3_code)
 join <@PREFIX>vm_user_info del  
   on (o.user_info_id = del.user_info_id )
join <@PREFIX>vm_order_status ost on o.order_status = ost.order_status_code
left join <@PREFIX>vm_country delcountry on (del.country = delcountry.country_3_code)
where ((from_unixtime(o.mdate) >= %s and from_unixtime(o.cdate) is null) or from_unixtime(o.mdate) >= %s ) and ost.order_status_id in (%s)
and ((from_unixtime(o.mdate) <= %s and from_unixtime(o.cdate) is null) or from_unixtime(o.mdate) <= %s )