sql联合查询

Posted netlws

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql联合查询相关的知识,希望对你有一定的参考价值。

(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))

 

以上是关于sql联合查询的主要内容,如果未能解决你的问题,请参考以下文章

sql如何一对多联合查询

sql联合查询UNION问题

求三表联合查询的SQL查询语句

求三表联合查询的SQL查询语句

多表联合查询:基于注解SQL

SQL联合查询:子表任一记录与主表联合查询