sql联合查询

时间:2021-10-16 05:36:08

(SELECT affairs.*
FROM affairs
 LEFT JOIN materials_details m ON affairs.AFFAIRID = m.AFFAIRID
WHERE (((m.EXAMPLEPATH IS NOT NULL
   AND m.EXAMPLEPATH <> ‘‘)
  OR (m.EMPTYTABLEPATH IS NOT NULL
   AND m.EMPTYTABLEPATH <> ‘‘))
 AND affairs.VALID = 1
 AND affairs.AF_PARENT = -1
 AND affairs.NodeType = 3
 AND affairs.AF_DEPARTMENT = 4)
GROUP BY affairs.AFFAIRID)
UNION
(SELECT *
FROM affairs
WHERE (AFFAIRID IN (
  SELECT ff.AF_PARENT
  FROM (
   SELECT AF_PARENT
   FROM affairs
   WHERE (AffairId IN (
     SELECT tt.AffairId
     FROM (
      SELECT a.AFFAIRID
      FROM affairs a
       LEFT JOIN materials_details m ON a.AFFAIRID = m.AFFAIRID
      WHERE (((m.EXAMPLEPATH IS NOT NULL
         AND m.EXAMPLEPATH <> ‘‘)
        OR (m.EMPTYTABLEPATH IS NOT NULL
         AND m.EMPTYTABLEPATH <> ‘‘))
       AND a.VALID = 1
       AND a.AF_DEPARTMENT = 4)
     ) tt
    )
    AND AF_PARENT > 0
    AND NodeType = 3)
  ) ff
 )
 AND NodeType = 1
 AND AF_PARENT = -1))
UNION
(SELECT *
FROM affairs
WHERE (AFFAIRID IN (
  SELECT uu.AF_PARENT
  FROM (
   SELECT AF_PARENT
   FROM affairs
   WHERE (AFFAIRID IN (
     SELECT ff.AF_PARENT
     FROM (
      SELECT a.AF_PARENT
      FROM affairs a
       LEFT JOIN materials_details m ON a.AFFAIRID = m.AFFAIRID
      WHERE (((m.EXAMPLEPATH IS NOT NULL
         AND m.EXAMPLEPATH <> ‘‘)
        OR (m.EMPTYTABLEPATH IS NOT NULL
         AND m.EMPTYTABLEPATH <> ‘‘))
       AND a.VALID = 1
       AND a.AF_PARENT > 0
       AND a.NodeType = 3
       AND a.AF_DEPARTMENT = 4)
      GROUP BY a.AFFAIRID
     ) ff
    )
    AND NodeType = 2
    AND AF_PARENT > 0)
  ) uu
 )
 AND NodeType = 1
 AND AF_PARENT = -1))