php 写sql,获取下一级的code

来源:互联网 发布:淘宝750海报图片 编辑:程序博客网 时间:2024/06/05 04:49
/// <summary>
/// 得到下一个ID  任何顶级分类的前缀是0001
/// </summary>
/// <param name="tableName">表名</param> ecs_category
/// <param name="fieldName">字段名</param> cat_code
/// <param name="prefix">前缀</param> 父类的cat_code
/// <param name="initValue">初始值</param> 父类的cat_coade.'0001'
/// <param name="whereSentence">WHERE子句</param> SUBSTRING(cat_code,1,LENGTH(前缀))=前缀 and LENGTH(cat_code)=LENGTH(初始值)
/// <returns>下一个ID字符串</returns>
function  GetNextID($parent_id)
{
$initValue="";//初始值
$prefix="";//前缀
$theSql = "";
if ($parent_id==0){
$prefix="";
$initValue="0001";
}else{
$sql="select cat_code from ecs_category where cat_id=$parent_id";
$prefix=$GLOBALS['db']->getOne($sql);
$initValue=$prefix."0001";
}


$theSql="SELECT CASE WHEN MAX(cat_code) IS NULL THEN '$initValue' ELSE
CONCAT('$prefix', LPAD(max(substring(cat_code,LENGTH('$prefix')+1,LENGTH('$initValue')-LENGTH('$prefix')))+1,LENGTH('$initValue')-LENGTH('$prefix'),'0'))   end
as NEXT_ID from ecs_category  where SUBSTRING(cat_code,1,LENGTH('$prefix'))='$prefix' and LENGTH(cat_code)=LENGTH('$initValue')";
$nextID=$GLOBALS['db']->getOne($theSql);
 
// echo $theSql;echo '<br/>';
// echo $nextID;echo '<br/>';

return $nextID;
}
0 0
原创粉丝点击