2016-05-13 173 views
-1

我需要返回operacao='DELETE'如果它存在,或者如果它不存在,則返回與現在返回相同的結果。現在查詢返回找到的第一個'operacao'。MySQL查詢和EXISTS子句

可以說,我需要知道operacao也'刪除'存在。

SELECT A.*, wf.RHID as rhidwf, wf.operacao as operacao 
from (
    SELECT 
    CONCAT(RHID) AS pk, RHID, CD_DOC_ID, SEQ, NR_DOCUMENTO, EMISSOR, 
    DT_EMISSAO, DT_VALIDADE 
    FROM rh_id_documentos 
    WHERE RHID='6' AND CD_DOC_ID='1' AND SEQ='1') A 
LEFT JOIN fo_on_workflow wf ON A.pk = wf.RHID 
GROUP BY A.RHID, A.CD_DOC_ID, A.SEQ; 
+1

這是非常不清楚。更好地展示一些示例。 –

+0

我創建了一個小提琴http://sqlfiddle.com/#!9/7704f4/1 –

回答

1
SELECT A.* 
    ,wf.RHID AS rhidwf 
    ,wf.operacao AS operacao 
FROM ( 

    SELECT CONCAT ('%',RHID,'>%') AS pk 
     ,RHID 
     ,CD_DOC_ID 
     ,SEQ 
     ,NR_DOCUMENTO 
     ,EMISSOR 
     ,DT_EMISSAO 
     ,DT_VALIDADE 
    FROM rh_id_documentos 
    WHERE RHID = '6' 
     AND CD_DOC_ID = '1' 
     AND SEQ = '1' 
) A 
LEFT JOIN fo_on_workflow wf ON wf.RHID LIKE A.pk 
ORDER BY operacao LIMIT 1