十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
我给你举个递归查询嵌套的例子,你看一下就明白了。
创新互联公司长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为汝州企业提供专业的成都网站建设、网站建设,汝州网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
select yr_student_info.name from yr_student_info where yr_student_info.school_id in (select yr_school_info.id from yr_school_info where yr_school_info.province='上海' and yr_school_info.city='上海')
概念说不一定理解,给你一个例子你自己参悟。
SELECT TEACHER
FROM C AS X
WHERE UNIQUE(SELECT TEACHER
FROM C AS Y
WHERE Y.TEACHER=X.TEACHER);
[sql] view plain copy
CREATE FUNCTION `getChildList`(rootId INT)
RETURNS varchar(1000)
BEGIN
DECLARE sChildList VARCHAR(1000);
DECLARE sChildTemp VARCHAR(1000);
SET sChildTemp =cast(rootId as CHAR);
WHILE sChildTemp is not null DO
IF (sChildList is not null) THEN
SET sChildList = concat(sChildList,',',sChildTemp);
ELSE
SET sChildList = concat(sChildTemp);
END IF;
SELECT group_concat(id) INTO sChildTemp FROM user_role where FIND_IN_SET(parentid,sChildTemp)0;
END WHILE;
RETURN sChildList;
END;
[sql] view plain copy
/*获取子节点*/
[sql] view plain copy
function sumShuzi($tree, $updateData = array()) {
$sum = 0;
// foreach($tree as $key = $item) { //这句话有毒
foreach($tree as $key = $item) {
if(isset($item['children'])) {
$oldPshuzi = $tree[$key]['Pshuzi'];
$tree[$key]['Pshuzi'] = sumShuzi($item['children'], $updateData);
if($oldPshuzi != $tree[$key]['Pshuzi']) {
$updateData[$item['id']] = array($tree[$key]['Pshuzi'], $tree[$key]['Pname_ch']);
}
}
$sum += $tree[$key]['Pshuzi'];
}
return $sum;
}
$tree = json_decode('[{"id":"1","Pid":"0","Pname_ch":"\u6e20\u9053\u90e8","Pshuzi":"1638000","children":[{"id":"4","Pid":"1","Pname_ch":"\u9500\u552e\u4e8c\u90e8","Pshuzi":"895000","children":[{"id":"13","Pid":"4","Pname_ch":"\u5468\u7ecf\u7406","Pshuzi":"28","children":[{"id":"28","Pid":"13","Pname_ch":"\u6e56\u5357","Pshuzi":"158000"},{"id":"35","Pid":"13","Pname_ch":"\u65b0\u7586","Pshuzi":"19000"}]},{"id":"40","Pid":"4","Pname_ch":"\u9648\u7ecf\u7406","Pshuzi":"5000"}]}]}]', true);
//$tree是具有父子关系的数据树
sumShuzi($tree, $updateData);
foreach ($updateData as $id = $item) {
$sql = "update your_table set Pshuzi={$item[0]} where id={$id}";
mysqli_query($db, $sql); //$db是你的数据库连接结果
}
你这个表设计有问题, 重设计个表pid 为院系 下级为班级
暂时想这么多,自己考虑吧