如何使用IF(LEFT JOIN中的LEN

时间:2015-10-13 13:33:42

标签: mysql

我正在尝试基于具有多个条件和LEN的IF语句创建连接,但我无法使其工作。

SELECT
    "Datafeed-WO"."Billing Cd" AS 'Billing Type',        
    IF(LEN("Datafeed-WO"."Zip")  = 6, 'CDA', 'USA') AS 'Country',
    "BudgetTbl"."BudYr" AS 'Budget Yr',
    "BudgetTbl"."Budget" AS 'Group Budget'

FROM "Datafeed-WO"

LEFT JOIN "BudgetTbl" 
   ON ( IF(LEN("Datafeed-WO"."Zip")  = 6, 'CDA', 'USA') = "BudgetTbl"."Country")  
      AND  ("Datafeed-WO"."Billing Cd"  = "BudgetTbl"."Billing Cd")

感谢您解决此问题的任何帮助!

1 个答案:

答案 0 :(得分:0)

您需要将所有引号('和“)更改为反引号(`)并尝试再次执行它。 此后,不要在表名和列名中包含空格也很重要。

希望这是你想要的:

select
    dw.`Billing Cd` as `Billing_Type`,       
    if(len(dw.`Zip`)  = 6, 'CDA', 'USA') as `Country`,
    bt.`BudYr` as `Budget_Yr`,
    bt.`Budget` as `Group_Budget`
from `Datafeed-WO` dw
left join `BudgetTbl` bt on (dw.`Billing Cd` = bt.`Billing Cd`) and ((len(dw.`Zip`) = 6 && bt.`Country` = 'CDA') || (len(dw.`Zip`) != 6 && bt.`Country` = 'USA'));