第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以有效和簡單的方式實現(xiàn)等級,父/子關(guān)系

以有效和簡單的方式實現(xiàn)等級,父/子關(guān)系

慕姐8265434 2019-08-05 15:24:50
以有效和簡單的方式實現(xiàn)等級,父/子關(guān)系我有一張桌子create table site(site_Id int(5),parent_Id int(5),site_desc varchar2(100));領(lǐng)域的意義:site_Id:網(wǎng)站的IDparent_Id:站點的父IDsite_desc:雖然與問題無關(guān),但它有網(wǎng)站的描述要求是,如果我有一個site_id作為輸入,我需要在網(wǎng)站下面標記所有ID。例如:                    A                   / \                  B   C                / | \ /\               D  E F G H              /\             I  J所有節(jié)點都是site_Id。該表包含如下數(shù)據(jù):Site_id  | Parent_ID  |  site_desc _________|____________|___________  A       |   -1       |     B       |    A       |  C       |    A       |  D       |    B       |  E       |    B       |  F       |    B       |  I       |    D       |  J       |    D       |......A是B和C的父級,依此類推。如果B是給定的輸入,那么查詢需要獲取D,E,I,F(xiàn),J它目前通過循環(huán)中的多個查詢來實現(xiàn),但我想在最少數(shù)量的查詢中實現(xiàn)這一點。我目前正在做的是::投票算法如下:Initially create a data set object which you will populate, by fetching data from the data base. Create a method which takes the parent id as parameter and returns its child nodes if present, and returns -1, if it doesnt have a child. Step1: Fetch all the rows, which doesn't have a parent(root) node.  Step2: Iterate through this result. For example if prod1 and prod2 are the initial returned nodes, in the resultset.  Iterating this RS we get prod1, and we insert a row in our DataSET obj.  Then we send the id of prod1 to getCHILD method, to get its child, and then again we iterate the returned resultset, and again call the getCHILD method, till we dont get the lowest node.我需要在數(shù)據(jù)模型約束中使用最佳優(yōu)化技術(shù)。如果您有任何建議,請隨時回答。請?zhí)岢鼋ㄗh。提前致謝。
查看完整描述

3 回答

?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

這恰好與你的問題你所描述:出給定的鄰接表的,你想獲得一個特定父的所有子節(jié)點 -也許在一維數(shù)組,你可以很容易地迭代。

您只需對數(shù)據(jù)庫進行一次調(diào)用即可完成此操作,但有一些問題:您必須從表中返回所有行。MySQL不支持遞歸查詢,因此您必須SELECT在應(yīng)用程序代碼中執(zhí)行此操作。

我只是重申我上面鏈接的答案,但基本上如果你返回一個結(jié)果集(可能來自PDOStatement->fetchAll(PDO::FETCH_ASSOC)或其他方法),格式如下:

Array(
    [0] => Array
    (
        [site_id] => A        [parent_id] => -1
        [site_desc] => testtext    )
    [1] => Array
    (
        [site_id] => B        [parent_id] => A        [site_desc] => testtext    )
    [2] => Array
    (
        [site_id] => C        [parent_id] => A        [site_desc] => testtext    )
    [3] => Array
    (
        [site_id] => D        [parent_id] => B        [site_desc] => testtext    )
    [4] => Array
    (
        [site_id] => E        [parent_id] => B        [site_desc] => testtext    )
    [5] => Array
    (
        [site_id] => F        [parent_id] => B        [site_desc] => testtext    )
    [6] => Array
    (
        [site_id] => I        [parent_id] => D        [site_desc] => testtext    )
    [7] => Array
    (
        [site_id] => J        [parent_id] => D        [site_desc] => testtext    ))

您可以site_id使用此遞歸函數(shù)檢索所有子/孫子/曾祖父/等等(如果您知道id):

function fetch_recursive($src_arr, $id, $parentfound = false, $cats = array()){
    foreach($src_arr as $row)
    {
        if((!$parentfound && $row['site_id'] == $id) || $row['parent_id'] == $id)
        {
            $rowdata = array();
            foreach($row as $k => $v)
                $rowdata[$k] = $v;
            $cats[] = $rowdata;
            if($row['parent_id'] == $id)
                $cats = array_merge($cats, fetch_recursive($src_arr, $row['site_id'], true));
        }
    }
    return $cats;}

例如,假設(shè)您想要檢索所有子項site_id D,您將使用如下函數(shù):

$nodelist = fetch_recursive($pdostmt->fetchAll(PDO::FETCH_ASSOC), 'D');print_r($nodelist);

輸出:

[0] => Array(
    [site_id] => D    [parent_id] => B    [site_desc] => testtext)[1] => Array(
    [site_id] => I    [parent_id] => D    [site_desc] => testtext)[2] => Array(
    [site_id] => J    [parent_id] => D    [site_desc] => testtext)

請注意,我們保留父母及其子女,孫子等的信息(無論嵌套深度如何)。


查看完整回答
反對 回復(fù) 2019-08-05
  • 3 回答
  • 0 關(guān)注
  • 481 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號