SQL Query使用反连接

时间:2013-01-02 06:49:26

标签: mysql sql select

我正在定义“匹配”以供稍后在某些php中使用,因此我在选择中定义了它。我似乎无法让这个查询工作...我得到一个解析错误。有什么想法吗?

SELECT 
    aif_id, 
    fee_source_id, 
    company_name_per_sedar, 
    document_filing_date, 
    NVL a_aif_remaining.aif_id, 0, 1 match
FROM a_aif, a_aif_remaining
LEFT JOIN a_aif_remaining ON a_aif_remaining.aif_id = a_aif.aif_id
ORDER BY aif_id DESC;

这也不起作用:

SELECT 
    aif_id, 
    fee_source_id, 
    company_name_per_sedar, 
    document_filing_date, 
    CASE IF a_aif_remaining.aif_id  THEN 0 ELSE 1 match
FROM a_aif, a_aif_remaining
LEFT JOIN a_aif_remaining ON a_aif_remaining.aif_id = a_aif.aif_id
ORDER BY aif_id DESC;

1 个答案:

答案 0 :(得分:3)

您收到该错误的原因是MATCH是保留字。你应该用反引号逃脱它。以下是保留字列表

试试这个,它应该是CASE WHEN...

SELECT aif_id, 
       fee_source_id, 
       company_name_per_sedar, 
       document_filing_date, 
       CASE 
            WHEN a_aif_remaining.aif_id  IS NULL -- you should have condition on this line
            THEN 0 
            ELSE 1 
       END `match`
FROM   a_aif
       LEFT JOIN a_aif_remaining 
          ON a_aif_remaining.aif_id = a_aif.aif_id
ORDER BY aif_id DESC;

如果列的值为NULL,您只需使用IF最小化代码),例如

SELECT aif_id, 
       fee_source_id, 
       company_name_per_sedar, 
       document_filing_date, 
       IF(a_aif_remaining.aif_id IS NULL, 0, 1) `match`
FROM   a_aif
       LEFT JOIN a_aif_remaining 
          ON a_aif_remaining.aif_id = a_aif.aif_id
ORDER BY aif_id DESC;