mysql递归查询(mysql中如何查询数据库中id不连续的数据)
本文目录
- mysql中如何查询数据库中id不连续的数据
- mysql如何递归汇总
- mysql查询一个节点的根节点
- php与mysql表中如何求递归求和汇总
- mysql递归查询连续时间的个数,在线等
- mysql查询一个表,实现递归查询
- mysql中的递归调用
- mysql递归查询,不用存储过程
mysql中如何查询数据库中id不连续的数据
只有几万条数据,那就好办多了,列举个笨方法如下
如果要一次性查出来,需要用递归了,因为不知道你中间的断点会有多少个,所以建议把这个表复制一份,直接对表进行删除操作
直接for循环
for($i
==1;$i《你的最大数据条数;$i++){
查询你的记录,是否有等于$i和$i+1的,如果这两条记录同时存在就删除$i编号
}
mysql如何递归汇总
单表自身关联查询,关联条件就是父节点pcode和code相等,查询字段包含pcode和sorce,将查询结果作为新表按pcode分组,用group by,查询字段是count记录数,这样就获取pcode的节点值,这是整体思路
mysql查询一个节点的根节点
java版的实际例子。类同你说的情况
private void findChildList(AssetType parent,List《AssetType》 list){
String hql = "from AssetType a where a.parentAssetType.assetTypeId=? ORDER BY a.sort,a.assetTypeName asc";
List《AssetType》 childList = this.assetTypeDao
.getEntityManager()
.createQuery(hql)
.setParameter(1, parent.getAssetTypeId())
.getResultList();
int size = childList.size();
if(size》0){
for (int i = 0; i 《 size; i++) {
AssetType assetType = childList.get(i);
List《AssetType》 childs = assetType.getChildAssetType();
if(childs.size()》0){
list.addAll(childs);
this.findChildList(assetType, list);//递归查询节点的子节点
}
}
}
}
php与mysql表中如何求递归求和汇总
function sumShuzi(&$tree, &$updateData = array()) {
$sum = 0;
// foreach($tree as $key =》 $item) { //这句话有毒
foreach($tree as $key =》 &$item) {
if(isset($item)) {
$oldPshuzi = $tree;
$tree, $updateData);
if($oldPshuzi != $tree) {
$updateData);
}
}
$sum += $tree;
}
return $sum;
}
$tree = json_decode(’’, true);
//$tree是具有父子关系的数据树
sumShuzi($tree, $updateData);
foreach ($updateData as $id =》 $item) {
$sql = "update your_table set Pshuzi={$item} where id={$id}";
mysqli_query($db, $sql); //$db是你的数据库连接结果
}
mysql递归查询连续时间的个数,在线等
SELECT COUNT(*) FROM tablename WHERE (username = ’Jack’) and
(DATE_ADD(sign_date, INTERVAL 1 DAY) IN
(SELECT sign_date FROM tablename WHERE (username = ’Jack’))
)
这里么有考虑5号星期五签到8号星期一签到也是连续签到的情况,抛砖引玉,你已经可以完成了。
你可以需要用到的函数:DAYOFWEEK
mysql查询一个表,实现递归查询
给你个网上写的比较好的例子:
方法一:利用函数来得到所有子节点号。
创建一个function getChildLst, 得到一个由所有子节点号组成的字符串.
mysql》 delimiter //
mysql》
mysql》 CREATE FUNCTION `getChildLst`(rootId INT)
-》 RETURNS varchar(1000)
-》 BEGIN
-》 DECLARE sTemp VARCHAR(1000);
-》 DECLARE sTempChd VARCHAR(1000);
-》
-》 SET sTemp = ’$’;
-》 SET sTempChd =cast(rootId as CHAR);
-》
-》 WHILE sTempChd is not null DO
-》 SET sTemp = concat(sTemp,’,’,sTempChd);
-》 SELECT group_concat(id) INTO sTempChd FROM treeNodes where FIND_IN_SET(pid,sTempChd)》0;
-》 END WHILE;
-》 RETURN sTemp;
-》 END
-》 //
Query OK, 0 rows affected (0.00 sec)
mysql》
mysql》 delimiter ;
使用我们直接利用find_in_set函数配合这个getChildlst来查找
mysql》 select getChildLst(1);
+-----------------+
| getChildLst(1) |
+-----------------+
| $,1,2,3,4,5,6,7 |
+-----------------+
1 row in set (0.00 sec)
mysql》 select * from treeNodes
-》 where FIND_IN_SET(id, getChildLst(1));
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 1 | A | 0 |
| 2 | B | 1 |
| 3 | C | 1 |
| 4 | D | 2 |
| 5 | E | 2 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
7 rows in set (0.01 sec)
mysql》 select * from treeNodes
-》 where FIND_IN_SET(id, getChildLst(3));
+----+----------+------+
| id | nodename | pid |
+----+----------+------+
| 3 | C | 1 |
| 6 | F | 3 |
| 7 | G | 6 |
+----+----------+------+
3 rows in set (0.01 sec)
mysql中的递归调用
首先创建一个熟悉的机构表
插入几条测试数据:
union all上面的是初始化语句,只会执行一次,查到了 开发部 这一行记录。
接下来下面的join会用初始化的语句去原来的organization表去join获取所有 开发部的子部门 ,然后再用这些 子部门 去join更下面的部门。
执行的结果如下:
如下想查询开发部的所有上级部门的话上面的递归查询语句简单改一下就可以了:
执行结果如下:
Recursive Common Table Expression ’temp’ can contain neither
aggregation nor window functi*** in recursive query block
mysql
mysql对递归的深度是有限制的,默认的递归深度是1000。
可以通过 show variables like ’cte_max_recursion_depth’; 进行查看
也可以通过select语句最大执行时间对递归加以显示, show variables lile ’max_execution_time’;
mysql递归查询,不用存储过程
--创建表
DROP TABLE IF EXISTS `t_areainfo`;
CREATE TABLE `t_areainfo` (
`id` int(11) NOT ’0’ AUTO_INCREMENT,
`level` int(11) DEFAULT ’0’,
`name` varchar(255) DEFAULT ’0’,
`parentId` int(11) DEFAULT ’0’,
`status` int(11) DEFAULT ’0’,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=65 DEFAULT CHARSET=utf8;
--初始数据
INSERT INTO `t_areainfo` VALUES (’1’, ’0’, ’中国’, ’0’, ’0’);
INSERT INTO `t_areainfo` VALUES (’2’, ’0’, ’华北区’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’3’, ’0’, ’华南区’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’4’, ’0’, ’北京’, ’2’, ’0’);
INSERT INTO `t_areainfo` VALUES (’5’, ’0’, ’海淀区’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’6’, ’0’, ’丰台区’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’7’, ’0’, ’朝阳区’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’8’, ’0’, ’北京XX区1’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’9’, ’0’, ’北京XX区2’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’10’, ’0’, ’北京XX区3’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’11’, ’0’, ’北京XX区4’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’12’, ’0’, ’北京XX区5’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’13’, ’0’, ’北京XX区6’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’14’, ’0’, ’北京XX区7’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’15’, ’0’, ’北京XX区8’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’16’, ’0’, ’北京XX区9’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’17’, ’0’, ’北京XX区10’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’18’, ’0’, ’北京XX区11’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’19’, ’0’, ’北京XX区12’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’20’, ’0’, ’北京XX区13’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’21’, ’0’, ’北京XX区14’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’22’, ’0’, ’北京XX区15’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’23’, ’0’, ’北京XX区16’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’24’, ’0’, ’北京XX区17’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’25’, ’0’, ’北京XX区18’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’26’, ’0’, ’北京XX区19’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’27’, ’0’, ’北京XX区1’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’28’, ’0’, ’北京XX区2’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’29’, ’0’, ’北京XX区3’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’30’, ’0’, ’北京XX区4’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’31’, ’0’, ’北京XX区5’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’32’, ’0’, ’北京XX区6’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’33’, ’0’, ’北京XX区7’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’34’, ’0’, ’北京XX区8’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’35’, ’0’, ’北京XX区9’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’36’, ’0’, ’北京XX区10’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’37’, ’0’, ’北京XX区11’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’38’, ’0’, ’北京XX区12’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’39’, ’0’, ’北京XX区13’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’40’, ’0’, ’北京XX区14’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’41’, ’0’, ’北京XX区15’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’42’, ’0’, ’北京XX区16’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’43’, ’0’, ’北京XX区17’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’44’, ’0’, ’北京XX区18’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’45’, ’0’, ’北京XX区19’, ’4’, ’0’);
INSERT INTO `t_areainfo` VALUES (’46’, ’0’, ’xx省1’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’47’, ’0’, ’xx省2’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’48’, ’0’, ’xx省3’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’49’, ’0’, ’xx省4’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’50’, ’0’, ’xx省5’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’51’, ’0’, ’xx省6’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’52’, ’0’, ’xx省7’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’53’, ’0’, ’xx省8’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’54’, ’0’, ’xx省9’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’55’, ’0’, ’xx省10’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’56’, ’0’, ’xx省11’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’57’, ’0’, ’xx省12’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’58’, ’0’, ’xx省13’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’59’, ’0’, ’xx省14’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’60’, ’0’, ’xx省15’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’61’, ’0’, ’xx省16’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’62’, ’0’, ’xx省17’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’63’, ’0’, ’xx省18’, ’1’, ’0’);
INSERT INTO `t_areainfo` VALUES (’64’, ’0’, ’xx省19’, ’1’, ’0’);
采用function获取所有子节点的id
--查询传入areaId及其以下所有子节点
DROP FUNCTION IF EXISTS queryChildrenAreaInfo;
CREATE FUNCTION `queryChildrenAreaInfo` (areaId INT)
RETURNS VARCHAR(4000)
BEGIN
DECLARE sTemp VARCHAR(4000);
DECLARE sTempChd VARCHAR(4000);
SET sTemp = ’$’;
SET sTempChd = cast(areaId as char);
WHILE sTempChd is not NULL DO
SET sTemp = CONCAT(sTemp,’,’,sTempChd);
SELECT group_concat(id) INTO sTempChd FROM t_areainfo where FIND_IN_SET(parentId,sTempChd)》0;
END WHILE;
return sTemp;
END;
--调用方式
select queryChildrenAreaInfo(1);
select * from t_areainfo where FIND_IN_SET(id, queryChildrenAreaInfo(1));