如何使用sql从这样的表中获取数据?

时间:2013-05-05 14:41:17

标签: mysql sql database

从下表中,我怎么可能得到这样的数据: 我的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);

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

答案 2 :(得分: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>";
}
?>