2013-05-05 32 views
0

從下面的表格中,我怎麼可能得到這樣的數據: 我的sql知識僅限於select和其他基本的東西。如何做一個sql來從這樣的表中獲取數據?

Heading 1 Eg: Kitchenware 
Heading 2 Eg: Knives 
    Heading 3 Eg: Butter Knives 
    Item: Cut em all 
    Item: Cull em all 
    Item: Smear em all 
    Heading 3 Eg: Meat Knives 
    Item: Cut em meat 
    Item: Cull em meat 
    Item: Smear em meat 

1級和2級是標題,不能放置物品。 3級可以容納物品。等級4是項目。有可能做到以上幾點。有時候,3級可能會級別後1

"id" "name"   "description"    "level" "parent" "country" "maxLevel" 
"1"  "Kitchenware" "Kitchenware description" "1"  "0"   "US"  "0" 
"2"  "Knives"  "All our knives"   "2"  "1"   "US"  "0" 
"3"  "Butter Knives" "All Butter Knives"   "3"  "2"   "US"  "0" 
"4"  "Cut em all" "Cut em all"    "4"  "3"   "US"  "0" 
"5"  "Cull em all" "Cull em all"    "4"  "3"   "US"  "0" 
"6"  "Smear em all" "Smear em all"    "4"  "3"   "US"  "0" 
"7"  "Meat Knives" "All Meat Knives"   "3"  "2"   "US"  "0" 
"8"  "Cut em meat" "Cut em meat"    "4"  "7"   "US"  "0" 
"9"  "Cull em meat" "Cull em meat"    "4"  "7"   "US"  "0" 
"10" "Smear em meat" "Smear em meat"    "4"  "7"   "US"  "0" 

表創建

CREATE TABLE `products` (
    `id` INT(10) NULL AUTO_INCREMENT, 
    `name` VARCHAR(50) NULL DEFAULT NULL, 
    `description` VARCHAR(240) NULL DEFAULT NULL, 
    `level` TINYINT(1) NULL DEFAULT '0', 
    `parent` INT(10) NULL DEFAULT '0', 
    `country` VARCHAR(2) NULL DEFAULT NULL, 
    `maxLevel` INT(1) NULL DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) 
COLLATE='utf8_general_ci' 
ENGINE=MyISAM; 

表數據

INSERT IGNORE INTO `products` (`id`, `name`, `description`, `type`, `parent`, `country`, `maxLevel`) VALUES 
    (1, 'Kitchenware', 'Kitchenware description', 1, 0, 'US', 0), 
    (2, 'Knives', 'All our knives', 2, 1, 'US', 0), 
    (3, 'Butter Knives', 'All Butter Knives', 3, 2, 'US', 0), 
    (4, 'Cut em all', 'Cut em all', 4, 3, 'US', 0), 
    (5, 'Cull em all', 'Cull em all', 4, 3, 'US', 0), 
    (6, 'Smear em all', 'Smear em all', 4, 3, 'US', 0), 
    (7, 'Meat Knives', 'All Meat Knives', 3, 2, 'US', 0), 
    (8, 'Cut em meat', 'Cut em meat', 4, 7, 'US', 0), 
    (9, 'Cull em meat', 'Cull em meat', 4, 7, 'US', 0), 
    (10, 'Smear em meat', 'Smear em meat', 4, 7, 'US', 0); 
+0

見http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ – Barmar 2013-05-05 14:42:56

+0

@Barmar我一直用這個,自上午上面的文章,然後貼在這裏:) – Norman 2013-05-05 14:48:35

回答

0

對於建築必須使用服務器編碼hieraric樹像PHP一樣。 顯示的替代方法是根據記錄級別構建縮進字符。

+0

這在技術上是正確的,因爲SQL總是返回具有相同行數和層次結構信息的表,儘管它可以按照有用的方式排序數據,如Tall boY所示。我打算+1,因爲我認爲這不值得downvote – Basic 2013-05-05 15:03:06

+0

所以你說我不能在一個聲明中得到它,我需要幾個sql語句來做到這一點? – Norman 2013-05-05 16:06:43

+0

在MySQL中,你可以通過「存儲過程」或實現服務器代碼來獲得它。我認爲你必須使用遞歸函數來構建它,並且SQL語句不支持此功能。 在服務器代碼中,我會根據獲取所有記錄的結果構建一個hieraric數組。 – Mamuz 2013-05-05 18:03:02

0

你可以建立一個表像這樣

H1      H2     H3   Item: 
Kitchenware   Knives   Butter Knives Cut em all 
Kitchenware   Knives   Meat Knives  Cut em all 
Kitchenware   Knives   Butter Knives Smear em meat 

,那麼你可以很容易地選擇基於h1 or h2 or h3

而且表結構是這樣的。

CREATE TABLE `products` (
`id` INT(10) NULL AUTO_INCREMENT, 
`h1` VARCHAR(50) NULL DEFAULT NULL, 
`h2` VARCHAR(240) NULL DEFAULT NULL, 
`h3` VARCHAR(240) NULL DEFAULT NULL, 
`Item:` VARCHAR(240) NULL DEFAULT NULL, 
`level` TINYINT(1) NULL DEFAULT '0', 
`parent` INT(10) NULL DEFAULT '0', 
`country` VARCHAR(2) NULL DEFAULT NULL, 
`maxLevel` INT(1) NULL DEFAULT NULL, 
PRIMARY KEY (`id`) 
) 
COLLATE='utf8_general_ci' 
ENGINE=MyISAM; 

SQl的類似SELECT from table WHERE h1=somthing AND h3=somthing

+0

嗯......不能改變桌子,必須堅持。 – Norman 2013-05-05 16:02:16

0

我最後只是這樣。不是最好的,但工作。他們說從函數內部調用函數不是一個好主意。

<?php 

$conn = connect(); 

$what = 1; 

$stmt = $conn->prepare("select id as id, name as name, description as description from products where level = :what and country = 'US'"); 
$stmt->bindParam(':what', $what); 
$stmt->execute(); 

$rows = $stmt->rowCount(); 

//echo 'Rows found '.$rows.' hey<br>'; 

while($row = $stmt->fetch()) 
{ 
    echo $row['name']." ".$row['id']; 
    echo "<br><br>"; 

    $next = $conn->prepare("select id as id, level as level, name as name, description as description from products where level > :what and country = 'US' and parent = :parent"); 
    $next->bindParam(':what', $row['id']); 
    $next->bindParam(':parent', $row['id']); 
    $next->execute(); 

    while($row = $next->fetch()) 
    { 
     if($row['level'] == '2') 
     { 
      echo $row['name']." Id: ".$row['id']." Level: ".$row['level']."<br>"; 
      fetchElements($row['level'],$row['id']); 
     }else if($row['level'] == '3') 
     { 
      echo $row['name']." Id: ".$row['id']." Level: ".$row['level']."<br>"; 
      fetchElements($row['level'],$row['id']); 
     } 
    } 
} 

function fetchElements($one,$two) 
{ 
    global $conn; //I feel this is incorrect. $conn is defined up there once. 
    $elements = $conn->prepare("select id as id, level as level, name as name, description as description from products where level > :what and parent = :parent and country = 'US'"); 
    $elements->bindParam(':what', $one); 
    $elements->bindParam(':parent', $two); 
    $elements->execute(); 

    while($row = $elements->fetch()) 
    { 
     echo $row['name']." Id: ".$row['id']." Level: ".$row['level']."<br>"; 
     if($one == 2) 
     { 
      fetchElements($row['level'],$row['id']); 
     } 
    } 
    echo "<br>"; 
} 
?> 
相關問題