MySQL如何根据条件填充查询列?
我有一个为CSV导出准备一些客户数据的查询:MySQL如何根据条件填充查询列?,mysql,Mysql,我有一个为CSV导出准备一些客户数据的查询: SELECT cu.*, ct1.CON_FirstName AS billing_FirstName , ct1.CON_MiddleI AS billing_MiddleI , ct1.CON_LastName AS billing_LastName , ct1.CON_Address1 AS billing_CON_Address1 , ct1.CON_Address2 AS billing_CON_Address2 , ct1.CON_Ci
SELECT cu.*,
ct1.CON_FirstName AS billing_FirstName , ct1.CON_MiddleI AS billing_MiddleI , ct1.CON_LastName AS billing_LastName , ct1.CON_Address1 AS billing_CON_Address1 , ct1.CON_Address2 AS billing_CON_Address2 , ct1.CON_City AS billing_CON_City , ct1.CON_State AS billing_CON_State , ct1.CON_Province AS billing_Province , ct1.CON_Country AS billing_Country , ct1.CON_Zip AS billing_Zip , ct1.CON_Phone1 AS billing_Phone1 , ct1.CON_Phone2 AS billing_Phone2 , ct1.CON_Text AS billing_Text , ct1.CON_FriendlyName AS billing_FriendlyName , ct1.CON_Email AS billing_Email,
ct2.CON_FirstName AS shipping_FirstName , ct2.CON_MiddleI AS shipping_MiddleI , ct2.CON_LastName AS shipping_LastName , ct2.CON_Address1 AS shipping_CON_Address1 , ct2.CON_Address2 AS shipping_CON_Address2 , ct2.CON_City AS shipping_CON_City , ct2.CON_State AS shipping_CON_State , ct2.CON_Province AS shipping_Province , ct2.CON_Country AS shipping_Country , ct2.CON_Zip AS shipping_Zip , ct2.CON_Phone1 AS shipping_Phone1 , ct2.CON_Phone2 AS shipping_Phone2 , ct2.CON_Text AS shipping_Text , ct2.CON_FriendlyName AS shipping_FriendlyName , ct2.CON_Email AS shipping_Email
FROM CUSTOMERS cu
LEFT JOIN CONTACT ct1 ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type = 1
LEFT JOIN CONTACT ct2 ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type = 2
GROUP BY cu.CU_CustomerID
ORDER BY cu.CU_CustCatId DESC
我的问题是,第三个CON_Type值为3,这意味着联系信息是账单和发货。i、 e.一些用户在联系人表中有两个条目,而另一些用户[其发货和账单相同]只有一个条目
对于只有CON_Type=3的记录,如何填充shipping和billing列 还没有测试过,但我认为:
LEFT JOIN CONTACT ct1 ON ct1.CON_CustomerID = cu.CU_CustomerID AND (ct1.CON_Type = 1 OR ct1.CON_Type = 3)
LEFT JOIN CONTACT ct2 ON ct2.CON_CustomerID = cu.CU_CustomerID AND (ct2.CON_Type = 2 OR ct2.CON_Type = 3)
我还没有测试过,但我认为:
LEFT JOIN CONTACT ct1 ON ct1.CON_CustomerID = cu.CU_CustomerID AND (ct1.CON_Type = 1 OR ct1.CON_Type = 3)
LEFT JOIN CONTACT ct2 ON ct2.CON_CustomerID = cu.CU_CustomerID AND (ct2.CON_Type = 2 OR ct2.CON_Type = 3)
我还没有测试过,但我认为:
LEFT JOIN CONTACT ct1 ON ct1.CON_CustomerID = cu.CU_CustomerID AND (ct1.CON_Type = 1 OR ct1.CON_Type = 3)
LEFT JOIN CONTACT ct2 ON ct2.CON_CustomerID = cu.CU_CustomerID AND (ct2.CON_Type = 2 OR ct2.CON_Type = 3)
我还没有测试过,但我认为:
LEFT JOIN CONTACT ct1 ON ct1.CON_CustomerID = cu.CU_CustomerID AND (ct1.CON_Type = 1 OR ct1.CON_Type = 3)
LEFT JOIN CONTACT ct2 ON ct2.CON_CustomerID = cu.CU_CustomerID AND (ct2.CON_Type = 2 OR ct2.CON_Type = 3)
如果我理解正确,我会在on条款中使用:
from子句看起来像:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type IN (1, 3) LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type IN (2, 3)
如果要排除类型1和2以及其他类型,则可以将其过滤掉:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type = 3 LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type = 3
WHERE NOT EXISTS (SELECT 1
FROM CUSTOMERS cu2
WHERE cu2.CU_CustomerID = cu.CU_CustomerID and
cu2.CON_Type <> 3
)
如果我理解正确,我会在on条款中使用:
from子句看起来像:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type IN (1, 3) LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type IN (2, 3)
如果要排除类型1和2以及其他类型,则可以将其过滤掉:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type = 3 LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type = 3
WHERE NOT EXISTS (SELECT 1
FROM CUSTOMERS cu2
WHERE cu2.CU_CustomerID = cu.CU_CustomerID and
cu2.CON_Type <> 3
)
如果我理解正确,我会在on条款中使用:
from子句看起来像:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type IN (1, 3) LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type IN (2, 3)
如果要排除类型1和2以及其他类型,则可以将其过滤掉:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type = 3 LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type = 3
WHERE NOT EXISTS (SELECT 1
FROM CUSTOMERS cu2
WHERE cu2.CU_CustomerID = cu.CU_CustomerID and
cu2.CON_Type <> 3
)
如果我理解正确,我会在on条款中使用:
from子句看起来像:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type IN (1, 3) LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type IN (2, 3)
如果要排除类型1和2以及其他类型,则可以将其过滤掉:
FROM CUSTOMERS cu LEFT JOIN
CONTACT ct1
ON ct1.CON_CustomerID = cu.CU_CustomerID AND ct1.CON_Type = 3 LEFT JOIN
CONTACT ct2
ON ct2.CON_CustomerID = cu.CU_CustomerID AND ct2.CON_Type = 3
WHERE NOT EXISTS (SELECT 1
FROM CUSTOMERS cu2
WHERE cu2.CU_CustomerID = cu.CU_CustomerID and
cu2.CON_Type <> 3
)
什么意思只有con_类型=3?什么意思只有con_类型=3?什么意思只有con_类型=3?什么意思只有con_类型=3?有时候最简单的答案就是坐在你的正前方,嗯?@SeanKimball,这是常有的事。戈登的IN建议也很有用,尤其是当你在同一列中有两个以上的值时。有时候最简单的答案就是坐在你面前,嗯?@SeanKimball,这种情况经常发生。戈登的IN建议也很有用,尤其是当你在同一列中有两个以上的值时。有时候最简单的答案就是坐在你面前,嗯?@SeanKimball,这种情况经常发生。戈登的IN建议也很有用,尤其是当你在同一列中有两个以上的值时。有时候最简单的答案就是坐在你面前,嗯?@SeanKimball,这种情况经常发生。Gordon的IN建议也非常有用,尤其是当同一列的值超过2个时。