mysql - 如何在 LEFT JOIN 中使用 IF(LEN

标签 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")

感谢您帮助解决这个问题!

最佳答案

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

希望这是你想要的:

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'));

关于mysql - 如何在 LEFT JOIN 中使用 IF(LEN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33104087/

相关文章:

mySQL - 取决于其他列的唯一性约束

mysql - Drupal 错误 PDOException : SQLSTATE[HY000] [2002] Can't connect to local MySQL

php - 变量不会在 PHP 中从表单传递到电子邮件

php - 将关系型与非关系型混合(MySQL 和 MongoDB)

php - 在 2 个表中搜索唯一条目,然后更改数据

php - 在 php 中解析非常大的 XML 文件

mysql - 拒绝mysql中的某些查询

MySQL,存储过程,差异设置局部变量

php - 通过 FTP 上传文件 - 安全

mysql - Mysql中联合三张表