lundi 17 octobre 2016

MySQL join a table to another table only if

I have a little complicated query here. I have to select columns by names cant just user 'orders.*. because i have multiple columns from different tables with the same name.

What I'm trying to do is to select specific fields from orders,users,payment_methods_translation and join bank_accounts_translation only if the orders.payment_method_id = '3'

select 
  orders.id as orderid,
  orders.final_total,
  orders.user_id,
  orders.auto_cancel,
  users.id as userid,
  users.first_name, 
  payment_methods_translation.payment_method_id,
  payment_methods_translation.name 
from 
  orders,
  users, 
  payment_methods_translation 
where 
  orders.id='$id' 
  and orders.user_id = users.id 
  and orders.payment_method_id = payment_methods_translation.payment_method_id 
 and orders.auto_cancel='1' 
JOIN bank_accounts_translation ON (orders.payment_method_id='3' and orders.bank_id = bank_accounts_translation.bank_account_id)

But I get a mysql error. So how can I select all the fields from bank_accounts_translation only if orders.payment_method_id = '3' and orders.bank_id = bank_accounts_translation.bank_id

Aucun commentaire:

Enregistrer un commentaire