问题描述
在没有b.table匹配项的情况下,MYSQL左连接A.table和b.table,同时保留a.table id.
MYSQL Left join A.table and b.table while retaining a.table id when there is no b.table match.
SELECT * FROM sales_customer a LEFT JOIN sales_contact b
ON a.customer_id = b.customer_id
ORDER BY CASE WHEN company_name = '' THEN lname ELSE company_name END ASC
给我这个:
Array (
[0] => 54
[customer_id] =>
)
没有b.table匹配项时.
When there is no b.table match.
我需要这个:
Array (
[0] => 54
[customer_id] => 29
)
有什么建议吗?
以下解决方案有效.感谢您的帮助.
The solution below worked. Thanks for your help.
SELECT *,COALESCE(a.customer_id,0)AS customer_id FROM sales_customer a左外部联接sales_contact b ON a.customer_id = b.customer_id ORC BY FOR CASE WHEN company_name =``THEn lname ELSE company_name END ASC
SELECT *, COALESCE(a.customer_id, 0) AS customer_id FROM sales_customer a LEFT OUTER JOIN sales_contact b ON a.customer_id = b.customer_id ORDER BY CASE WHEN company_name = '' THEN lname ELSE company_name END ASC
推荐答案
像这样使用它:
SELECT
*,
COALESCE(b.customer_id, 0) AS customer_id
FROM sales_customer a
LEFT JOIN sales_contact b ON a.customer_id = b.customer_id
ORDER BY
CASE WHEN company_name = '' THEN lname ELSE company_name END ASC
这篇关于MYSQL左连接A.table和b.table,同时保留a.table id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!