Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 适用于大型集合,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的ari连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到ari。谢谢你。@Sha_Mysql_Join_Count_Group By - Fatal编程技术网

Mysql 适用于大型集合,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的ari连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到ari。谢谢你。@Sha

Mysql 适用于大型集合,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的ari连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到ari。谢谢你。@Sha,mysql,join,count,group-by,Mysql,Join,Count,Group By,适用于大型集合,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的ari连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到ari。谢谢你。@ShaunScovil。第二个版本中有一个错误,现在已经修复并测试。太棒了,谢谢戈登!(以为我在那里疯了一分钟……:-)+1。第二种方法对于大型集合应该更有效,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?R


适用于大型集合,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的
ari
连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到
ari
。谢谢你。@ShaunScovil。第二个版本中有一个错误,现在已经修复并测试。太棒了,谢谢戈登!(以为我在那里疯了一分钟……:-)+1。第二种方法对于大型集合应该更有效,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的
ari
连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到
ari
。谢谢你。@ShaunScovil。第二个版本中有一个错误,现在已经修复并测试。太棒了,谢谢戈登!(以为我在那里疯了一分钟……:-)+1。第二种方法对于大型集合应该更有效,因为它避免了具体化获取每个可能的RootId值的计数的内联视图。但是不需要有一个小组吗?RootId上的
ari
连接将返回多行,不是吗?@spencer7593。否,不需要分组依据。只是正确的连接。最多只能有一个匹配到
ari
。谢谢你。@ShaunScovil。第二个版本中有一个错误,现在已经修复并测试。太棒了,谢谢戈登!(以为我在那里会发疯一分钟……:-)
+-----------------------+
|         Assets        |
+-----------------------+
| Id |   Name  | RootId |
+----+---------+--------+
|  1 | Asset A |    1   |
+----+---------+--------+
|  2 | Asset B |    2   |
+----+---------+--------+
|  3 | Asset C |    3   |
+----+---------+--------+
|  4 | Asset D |    2   |
+----+---------+--------+
|  5 | Asset E |    3   |
+----+---------+--------+
|  6 | Asset F |    3   |
+----+---------+--------+
+----------------------------------+
|              Assets              |
+----------------------------------+
| Id |   Name  | RootId | RootName |
+----+---------+--------+----------+
|  4 | Asset D |    2   |  AssetB  |
+----+---------+--------+----------+
+----------------------------------+
|              Assets              |
+----------------------------------+
| Id |   Name  | RootId | RootName |
+----+---------+--------+----------+
|  5 | Asset E |  null  |   null   |
+----+---------+--------+----------+
select id, name,
       (case when cnt = 2 then rootid end) as rootid,
       (case when cnt = 2 then ari.name end) as rootname
from assets a join
     (select rootid, count(*) as cnt
      from assets a
      group by rootid
     ) ri
     on a.rootid = ri.rootid left join
     assets ari
     on a.rootid = ari.id
where id = 4;
select a.id, a.name,
       (case when a.cnt = 2 then a.rootid end) as rootid,
       (case when a.cnt = 2 then ari.name end) as rootname
from (select a.*,
             (select count(*) from assets a2 where a2.rootid = a.rootid) as cnt
      from assets a
      where id = 4
     ) a left join
     assets ari
     on a.rootid = ari.id;
SELECT a.Id
     , a.Name
     , IF(q.cnt=2,q.RootId,NULL) AS RootId
     , IF(q.cnt=2,q.RootName,NULL) AS RootName
  FROM Assets a
  JOIN ( SELECT COUNT(1) AS cnt
              , r.RootId
              , r.RootName
           FROM Assets r
           JOIN Assets s
             ON s.RootId = r.RootId
          WHERE r.Id = 4
          GROUP BY r.RootId, r.RootName
       ) q
    ON q.Id = a.Id
SELECT a.Id
     , a.Name
     , IF(q.cnt=2,q.RootId,NULL) AS RootId
     , IF(q.cnt=2,q.RootName,NULL) AS RootName
  FROM Assets a
  JOIN ( SELECT COUNT(1) AS cnt
              , r.RootId
              , r.RootName
           FROM Assets r
           LEFT                                -- lef outer join
           JOIN Assets s
             ON s.RootId <=> r.RootId          -- nullsafe equality
          WHERE r.Id = 4
          GROUP BY r.RootId, r.RootName
       ) q
    ON q.Id = a.Id