Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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子查询结果?_Mysql_Sql - Fatal编程技术网

如何循环MySql子查询结果?

如何循环MySql子查询结果?,mysql,sql,Mysql,Sql,有4个实体:标记、问题、它们之间的连接表-问题有标记,最后一个是相关标记,除了id之外还有两个字段:主标记和子标记。Bouth是对Tag.id的引用 子查询返回多个结果,我需要循环它,但不知道如何循环 select t.id, t.name, count(question_has_tag.tag_id) as i from tag as t join question_has_tag on t.id = question_has_tag.tag_id where t.id =

有4个实体:标记、问题、它们之间的连接表-问题有标记,最后一个是相关标记,除了id之外还有两个字段:主标记和子标记。Bouth是对Tag.id的引用

子查询返回多个结果,我需要循环它,但不知道如何循环

select t.id, t.name, count(question_has_tag.tag_id) as i from tag as t
   join question_has_tag on t.id = question_has_tag.tag_id
   where t.id = 
     (select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1)
   group by t.id order by i desc;

无法在SQL中对子查询结果进行循环。。您可以在过程语言中使用循环,例如:在使用触发器时使用服务器端语言

无论如何,在您的例子中,您可以使用联接,而不是与子查询结果进行比较

select t.id, t.name, count(question_has_tag.tag_id) as i 
from tag as t
join question_has_tag on t.id = question_has_tag.tag_id
INNER JOIN  (
    select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1
) t2 t.id = t2.child_tag 
group by t.id order by i desc;
或者,如果您只需要一个结果,请限制结果

select t.id, t.name, count(question_has_tag.tag_id) as i 
from tag as t
join question_has_tag on t.id = question_has_tag.tag_id
where t.id =       (
    select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1
     ORDER BY child_tag 
     limit 1
)
group by t.id order by i desc;
或者也使用IN子句代替=

select t.id, t.name, count(question_has_tag.tag_id) as i 
from tag as t
join question_has_tag on t.id = question_has_tag.tag_id
where t.id IN      (
    select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1
)
group by t.id order by i desc;

无法在SQL中对子查询结果进行循环。。您可以在过程语言中使用循环,例如:在使用触发器时使用服务器端语言

无论如何,在您的例子中,您可以使用联接,而不是与子查询结果进行比较

select t.id, t.name, count(question_has_tag.tag_id) as i 
from tag as t
join question_has_tag on t.id = question_has_tag.tag_id
INNER JOIN  (
    select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1
) t2 t.id = t2.child_tag 
group by t.id order by i desc;
或者,如果您只需要一个结果,请限制结果

select t.id, t.name, count(question_has_tag.tag_id) as i 
from tag as t
join question_has_tag on t.id = question_has_tag.tag_id
where t.id =       (
    select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1
     ORDER BY child_tag 
     limit 1
)
group by t.id order by i desc;
或者也使用IN子句代替=

select t.id, t.name, count(question_has_tag.tag_id) as i 
from tag as t
join question_has_tag on t.id = question_has_tag.tag_id
where t.id IN      (
    select related_tag.child_tag from related_tag
     join tag as t on related_tag.main_tag = t.id
     where related_tag.main_tag = 1
)
group by t.id order by i desc;

其中..=(..)
其中。。在(..)
中。但是最好的方法是重写这个加入。注:仅限团体成员。欢迎参加SO。请参见:不
WHERE..=(..)
其中。。在(..)
中。但是最好的方法是重写这个加入。注:仅限团体成员。欢迎参加SO。请看:注意,没有订单的限制是相当有意义的。没有订单的限制是相当没有意义的