我这里有一个复杂的查询。我必须按名称选择列,不能只是用户'订单。*。因为我有来自不同表的多个具有相同名称的列。
我尝试做的是仅在订单,用户,payment_methods_translation中选择特定字段,并且仅在orders.payment_method_id ='3'
时才加入bank_accounts_translationSELECT
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)
但是我收到了一个mysql错误。那么,如果orders.payment_method_id ='3'和orders.bank_id = bank_accounts_translation.bank_id
,我怎样才能从bank_accounts_translation中选择所有字段答案 0 :(得分:0)
左连接应该这样做......我
LEFT JOIN表示返回先前列出的表中的所有记录,并且仅返回与要加入的表的on标准匹配的记录。
SELECT O.id as orderid
,O.final_total
,O.user_id
,O.auto_cancel
,u.id as userid
,u.first_name
,PMT.payment_method_id
,PMT.name
FROM orders O
INNER JOIN users U
ON O.user_id = U.id
INNER JOIN payment_methods_translation PMT
ON O.payment_method_id = PMT.payment_method_id
LEFT JOIN bank_accounts_translation BAT
ON O.payment_method_id='3'
and O.bank_id = BAT.bank_account_id
WHERE O.id='$id'
and O.auto_cancel='1'