0

我正在定义“匹配”以供稍后在某些 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;
4

1 回答 1

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;
于 2013-01-02T06:51:29.437 回答