码迷,mamicode.com
首页 > 数据库 > 详细

sql联合查询

时间:2019-09-12 18:36:14      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:val   left join   nio   mat   detail   and   联合   null   sel   

(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联合查询

标签:val   left join   nio   mat   detail   and   联合   null   sel   

原文地址:https://www.cnblogs.com/netlws/p/11513194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!