0

从下表中,我怎么可能得到这样的数据: 我的 sql 知识仅限于 select 和其他基本的东西。

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);
4

3 回答 3

0

要构建分层树,您必须使用 PHP 等服务器编码。显示的另一种方法是根据记录级别构建缩进字符。

于 2013-05-05T14:46:24.443 回答
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

于 2013-05-05T14:55:19.197 回答
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>";
}
?>
于 2013-05-07T07:40:03.017 回答